Questions tagged [function]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
1answer
17 views

Week 2 Lecture - Functions - Valid Triangle

This is from the functions lecture on a valid triangle. I'm not getting any error messages but I can't figure out why the bool valid_triangle (float x, float y, float z) function is not working. Any ...
0
votes
1answer
11 views

when I compile my program this bug appear. error: control may reach end of non-void function [-Werror,-Wreturn-type] }

bool check_valid_key (string s) { for (int i = 0, len = strlen(s); i < len; i++) { if (!isdigit(s[1])) return false; return true; } }
1
vote
1answer
34 views

pset 3 runoff help again

I still dont know what to put in for loop :( I still dont know what to put in the for loop for(int k = 0; k < voter_count; k++) { for (int j = 0; j < candidate_count; j++) { ...
0
votes
1answer
54 views

Pset2 Coleman-Lau Index Always Prints -15

I'm struggling with PSet 2 Readability. I don't want to cheat the answers because I am genuinely trying to learn how to think better and improve my logic. I so far have been able to get the letter ...
0
votes
1answer
26 views

Expected expression on “valid triangle” exercise? i thought the 'or' symbol was 2 lines ( || ) what have i done wrong here?

#include <stdio.h> #include <cs50.h> #include <string.h> #include <ctype.h> #include <math.h> // PROTOTYPE THE FUNCTON bool valid_triangle(float input1, float input2, ...
0
votes
2answers
26 views

Can anyone tell me what ive done wrong and breakdown how to properly prototype and deploy created functions?

code: #include <stdio.h> #include <cs50.h> #include <string.h> #include <ctype.h> #include <math.h> // PROTOTYPE THE FUNCTONS & DECLARE VARIABLES int multiply(int ,...
0
votes
1answer
22 views

What's in the bool function parenthesis?

hope everyone is staying safe. I just want to understand what's inside the parenthesis of the vote function in pset3 - Tideman: bool vote(int rank, string name, int ranks[]) What does it mean to ...
0
votes
1answer
159 views

too few arguments to function call, at least argument 'format' must be specified

I run the C program and I get the following errors. 4:20: error: too few arguments to function call, at least argument 'format' must be specified int x= get_int(); /usr/include/cs50.h:82:1: note: '...
0
votes
1answer
63 views

Question for Array in C (with function e.g. set_array)

While I was doing cs50 week2. I found something hard to understand. #include<stdio.h> void set_array(int array[4]); void set_int(int x); int main(void) { int a = 10; int b[4] = {0, 1,...
0
votes
1answer
39 views

Speller seg faults in unload function

I've tried to fiddle around with my unload function for a while and don't understand why I'm getting a seg fault. It seems that the unload function is running through completely at least once, but ...
1
vote
1answer
188 views

example positive.c explanation of function

1. // Abstraction and scope 2. 3. #include <cs50.h> 4. #include <stdio.h> 5. 6. int get_positive_int(string prompt); 7. 8. int main(void) 9. { 10. int ...
0
votes
1answer
19 views

Define buy or selling function

I am working on pset8 everything is working I have found a snippet of my code which is identical for buying and selling only difference is to subtract or add to current cash. Can someone point me in ...
0
votes
1answer
9 views

Analysing Expected runtime

I came across a question to implement an unbiased random function which outputs 0 or 1 using a biased output function which outputs 0 or 1 with probability P for outputting 1. And to predict how the ...
0
votes
1answer
166 views

PSET2 Vigenere: Control may reach end of non-void function error

I wrote a function to return the shift value for a certain character inputted. Where A, a = 0, B,b =1, etc. int shift(char c) { int asc; if (isupper(c)) { asc = atoi(&c) - 65;...
0
votes
1answer
21 views

About the functions

Why does this code not work? I want to understand why I need to create copies of a and b. I understood the whole idea, but the creating process is not clicking with me.
0
votes
1answer
76 views

declaring a function

Im trying to declare a new function to use in vigenere, I want to create a function that will check if an entire string is alphabetical (not just a single character in a string which is what the ...
0
votes
1answer
274 views

pow() function of a float

Why the pow function act like this?, shouldn't the computer consider any integer for example the number 3 as float like this 3.000etc? why it produce different results?
0
votes
1answer
39 views

PSET3- Game of fifteen, function move();

So, my move function is ALMOST working, but sometimes when swaping numbers both tile AND blank space shows up as a blank space...can you guys check my code? Thanks! Ps..Blank is a global variable, ...
0
votes
2answers
40 views

Give a Function access to earlier value

If the main block of code gets a value from the user, assigns it to an integer, and then changes that interger's value, is there a way for a function (created outside the main block of code) to access ...
0
votes
0answers
607 views

speller.c:217:5: error: redefinition of 'main' int main

I actually received two error messages: speller.c:217:5: error: redefinition of 'main' int main(int argc, char *argv[]) ^ speller.c:20:5: note: previous definition is here int main(int argc, ...
1
vote
1answer
73 views

Why do I get a wrong html file after executing a quote function? pset finance

I am implementing a quote function in application.py. But the result is ridiculous. After I enter a symbol to search for, I get an apology.html(saying Missing Username!), but this apology.html with ...
0
votes
1answer
18 views

Breakout / pause(function) slows the paddle

When I add pause(5) to the while loop, pause function makes the ball not zip around, it also seems to slow down the paddle responsiveness. What do I have to do to get around that? I do understand why ...
0
votes
1answer
55 views

Pset6 - Analyzer.py - Says Undeclared Function

I declared a function called load_words above init() to load in the words. However, when I call my function in init(), it gives the error name 'load_words' is undefined at the lines where I call ...
0
votes
1answer
25 views

PSET3:Find: Code works without the function, doesn't work with the function

So I am working on Selection Sort for Find, Problem Set 3. When I use a function I made called swap to exchange the values, the array is not sorted. Function snippet: if (min != i) { swap(values[...
0
votes
1answer
67 views

What does “using” a variable entail and why does this inconsistency come up? (pset1, greedy, general question)

During the greedy problem in pset1 I wrote this function: void greedy(int coin_type, int *change, int *coins) { while (*change / coin_type > 0) { *coins++; *change -= coin_type;...
1
vote
2answers
100 views

Pset4 recover.c compiles, but can't seem to print Jpgs at all

Am starting a new thread as I'm having trouble understanding what is wrong with the code.. I've added printf statements at the jpgCheck function and did not get a print. I suspect it's the function? ...
0
votes
2answers
336 views

(PSET 1) Creating functions for water.c. Dont understand how this function worked. (Contains pset answer)

I'm doing water.c of pset1 and wanted to use functions to practice and understand it more. After getting so many errors, I finally got it to work but I don't understand why and how it worked. How is '...
0
votes
1answer
197 views

Why does this recursive factorial function hit segmentation fault?

This is the recursive function I'm using to calculate the factorial of an int. It compiles fine but when I run it, the program hits segmentation fault. The function receives an int taken from the ...
0
votes
1answer
66 views

PSet 3 - Game of fifteen - draw tile gives weird output

I have not been able to draw the board properly. Whenever i run the code ~/workspace/pset3/fifteen/ $ ./fifteen 4 WELCOME TO GAME OF FIFTEEN 15 14 13 12 11 0 0 4196528 0 0 -186283616 32766 48 -...
0
votes
1answer
330 views

Use of functions and variable declarations outside main() on pset1 greedy

I have completed greedy from pset1 and I still doubts about the variable declarations and the function position. What is the difference if they are declared inside or outside the main function? #...
0
votes
1answer
70 views

CS50 pset 5 cannot load

I have been working on pset5 for a while. This compiles but load 0 words. http://pastebin.com/Kncw3V8g
0
votes
1answer
110 views

pset6 parse: char* pointers are right in parse but return wrong value

I am having trouble with this pset and I am using gdb to step through the function. Should I worry about the following observations or should I ignore them and assume my problems are from something ...
0
votes
1answer
31 views

Why does PrintName return void?

Okay so in the PrintName function, it contains a printf function. However, I read that printf returns a value which is the number of characters. Shouldn't PrintName hence return an int as well? Thanks!...
0
votes
1answer
432 views

Difference between Pass by Value and Pass by Reference (a practical example)

at the end of Doug's video on arrays he explains how variables get passed in function calls in comparison to how arrays work. void set_array(int array[4]); void set_int(int x); int main (void) { ...
0
votes
0answers
135 views

pset5-Testing load function separately2

I have been trying to test load function separately (Using Zamyla video for some guidance). I have watched this tape several times, but still unable to load function without compiler errors. I am ...
0
votes
1answer
496 views

pset5-testing load function

Ihave been trying to test my load function separately,since I have not implemented other functions,size,check,unload . I have got the following Clang error " expected identifier". I do not know how ...
0
votes
0answers
99 views

pset5 load function mallocs more new then i expected, am i building the trie wrong?

With the answer and tips from Cliff B i've managed to stop segfaults and loosing data in the memory, but still unsure about trie implementation. Here's the current code: bool load(const char* ...
0
votes
1answer
21 views

How to end with an unsuccesful memory allocation?

when allocating memory, it is recommended to manually check whether the allocation was successful (or more accurately, terminate the program if it wasn't). Main: int main (void) { char *c = ...
0
votes
1answer
24 views

Pointers in a function

Can someone please explain, why list-printing cycle with pointers works perfectly fine from main(), but refuses to work from separate function when called. #include <stdio.h> void listPrint (...
0
votes
1answer
24 views

How do I use my own function for ball movement and call it in main, rather than writing it in main

I've successfully written code inside the while loop in main that makes the ball bounce correctly. However, first I tried to call a function from main (just to make it easier to read), called ...
0
votes
1answer
70 views

space between function name and variables e.g. int main (void)

I checked the style guide https://manual.cs50.net/style/ and the C99 Standard https://en.wikipedia.org/wiki/C99 And neither explicitly states the best format but both implicitly write out main as so ...
2
votes
2answers
65 views

Could we declare the main function as “main () {}” rather than “int main (void) {}”?

In the examples in week 1, we declare the main function as follows: int main (void) { // code } Why do we use int? Is it wrong to use the following instead? main () { // code }
1
vote
1answer
420 views

scratch global functions

PSET01 Quick question about Scratch, can you set global scoped FUNCTIONS (or blocks?) that i can reuse across sprite scripts? Example would be creating a custom animation function for "next costume", ...
0
votes
1answer
139 views

Is there any general way to define a function?

I am not sure whether I am framing the question right or not. But please cooperate. In one of the videos from 'Week 2' 'Walkthrough' before starting the main program the 'GetPositiveInt' function was ...
0
votes
1answer
224 views

Modulo not working in caesar

When I run my code, anytime I try to use the modulo function, I get a strange symbol for an answer. I can't post it because it wouldn't download, but it is a square with 4 numbers inside. Why am I ...
0
votes
2answers
14 views

implementing functions in psets

From which problem set onwards should I start implementing functions? Is there any disadvantage in using functions when it comes to grading?
1
vote
1answer
178 views

is that a function pointer and how can it be used?

I've seen a typedef operation in the following format in signal function's man page: #include <signal.h> typedef void (*sighandler_t)(int); sighandler_t signal(int signum, ...
2
votes
1answer
4k views

Why can we pass a struct variable to a function by value but not an array?

As taught we can pass a whole "struct" by value to a function but we can't pass an array by value. Why? And, what if the passed struct contains an array as a member? Would it get passed completely (...
2
votes
1answer
395 views

How to list the functions of a library in the terminal?

One can search for the functions in a library by searching the internet, e.g. https://sites.google.com/site/chrowe/cs50-cheat-sheet or http://www.techonthenet.com/c_language/standard_library_functions/...
0
votes
1answer
1k views

Using function as a statement without using its return value

I just figured out that printf is a function of type int. int printf(const char* format, ...); And functions are supposed to return a value. I've never seen a line of code written like strlen("...