3
votes
Accepted
PSET5 Speller Load function skips the words from the dictionary
This while (fscanf(input, "%s", buffer) != EOF) executes an fscanf, followed immediately by this fscanf (input, "%s", buffer);, therefore it is processing every other word.
2
votes
Accepted
Why does this recursive factorial function hit segmentation fault?
The fact() function is a recursive function that takes a pointer to an int as input. However, the internal recursive call, return *a * fact(a - 1); is not passing a valid pointer to an int. It's clear ...
2
votes
PSET3:Find: Code works without the function, doesn't work with the function
C, by default, passes parameters to functions by value. That means when the function swap is called, two new variables, x and y are created, and the values of the integers are copied into them. When ...
2
votes
Accepted
space between function name and variables e.g. int main (void)
Nope, won't hurt a thing. It's just a matter of style, but has no effect on functionality.
The common practice though is not to have a space between main and the first parenthesis.
If this answers ...
2
votes
Accepted
Lecture 2 Arrays
The error you are getting is not saying that you are passing a double instead of a float, but that a double is expected. You are passing average, which is not a variable but a function. Your prototype ...
1
vote
PSET5 Speller Load function skips the words from the dictionary
Remove the fscanf (input, "%s", buffer); inside of while (fscanf(input, "%s", buffer) != EOF)
1
vote
Accepted
What does "using" a variable entail and why does this inconsistency come up? (pset1, greedy, general question)
The root of this problem lies in the order of operations. In C, postfix increment (++) binds more tightly than dereference (*). Thus, what the code *coins++; does is not to dereference coins and then ...
1
vote
Pset4 recover.c compiles, but can't seem to print Jpgs at all
I tried to isolate some of the codes in modular form and created a file just to check this code. It seem to be the problem.
Did I declare the function within the if statement wrongly?
#include <...
1
vote
Accepted
Pset4 recover.c compiles, but can't seem to print Jpgs at all
Just to be crystal clear:
if (data[3] != 0xe0 || data[3] != 0xe0 || data[3] != 0xe1 || data[3] != 0xe2 || data[3] != 0xe3 || data[3] != 0xe4 || data[3] != 0xe5 || data[3] != 0xe6 || data[3] != 0xe7 ||...
1
vote
Accepted
(PSET 1) Creating functions for water.c. Dont understand how this function worked. (Contains pset answer)
A real answer is further down
Take note to how I formatted your code according to CS50's style guide. Well formatted code is important if you expect others (or even yourself at a later date) to read ...
1
vote
Accepted
Week 2 Lecture - Functions - Valid Triangle
Just like the function printf, the valid_triangle function has to be called from within main in order to execute and produce results.
1
vote
Accepted
pset 3 runoff help again
Looking at the posted code and not sure of the full context of what you're trying to do, I'll take a run at it.
This is more of a question of what to take out, not what to put in.
The code is a for ...
1
vote
Accepted
What's in the bool function parenthesis?
No such thing as a silly question. It just means that you haven't learned it yet. That's all. ;-)
This is a function "signature". It defines what is expected to be passed to the function. int rank ...
1
vote
Accepted
too few arguments to function call, at least argument 'format' must be specified
In the current implementation of the functions of the header file cs50.h, we need at least one argument of the type char *, that is, a string, something similar to the function printf (), therefore to ...
1
vote
Accepted
Speller seg faults in unload function
Here's the issue:
if (cursor->next != NULL)
{
while (cursor->next != NULL)
The problem is that if cursor is ever null, cursor->next doesn't exist. That will cause a seg fault. ...
1
vote
example positive.c explanation of function
At line 10, the program calls get_positive_int with the string argument "Positive integer: "
This takes us to the function in line 15 and eventually to line 20, when get_int is called with the string ...
1
vote
Accepted
Define buy or selling function
I don't really see any practical reason or method to using the same function for both buy and sell. While there's overlap, the differences are enough that they should be separate functions. Combining ...
1
vote
Accepted
PSET2 Vigenere: Control may reach end of non-void function error
You're right. The compiler just knows that there's a possibility of getting to the end of the code without seeing a return statement, so it's unhappy.
However, your fix in the comments has a flaw. ...
1
vote
Accepted
declaring a function
You could test for "not alpha" and return false straightaway. Then, if you finish the entire loop, you know that all the chars were alpha.
for loop over string
if not alpha(char)
return ...
1
vote
PSET3- Game of fifteen, function move();
This is a common problem. The code is going off the edge of the board. Look at the following:
if (board[row][column+1] == blank){
Let's assume d=3 and column=2. What is at board[row][column+1]? ...
1
vote
Give a Function access to earlier value
You should store temporarily the value of height, to a new variable, so that you don't change the original value, like so:
void foo(int foo_height);
int main(void)
{
int height = get_int();
...
1
vote
Accepted
Give a Function access to earlier value
You have global var height. When you get value from user, assign it to height. Then copy it to new local var height1 which you can change as you want.
1
vote
Accepted
Why do I get a wrong html file after executing a quote function? pset finance
Look at the tab when you are entering the stock symbol. Does it say "Register"? Looks like quote.html started with the register.html code. It's still calling the register route on submit.
1
vote
Accepted
Pset6 - Analyzer.py - Says Undeclared Function
Based on how you write that, I assume both load_words and __init__ to be methods.
As you made load_words a method, you'd have to call it like self.load_words instead of just load_words.
1
vote
Accepted
PSet 3 - Game of fifteen - draw tile gives weird output
The board[][] array is a global variable. In both functions, the code is creating a shadow array called board[][].
int board[DIM_MAX][DIM_MAX];
By redeclaring the board array in each function, it ...
1
vote
Use of functions and variable declarations outside main() on pset1 greedy
The difference between where a variable is declared is scope. Strictly for this discussion, consider main to be a function, even though it is a special one.
A local variable is a variable declared ...
1
vote
Accepted
CS50 pset 5 cannot load
No words are loaded because the while loop never executes. The fscanf() call is attempting to store a word in dict_word, but the pointer dict_word was never initialized. It wasn't set to null and it ...
1
vote
Accepted
pset6 parse: char* pointers are right in parse but return wrong value
Notice the difference between the declaration of abs_path/query in main
char abs_path[LimitRequestLine + 1];
char query[LimitRequestLine + 1];
vs. the declaration in parse.
bool parse(const char* ...
1
vote
Accepted
Why does PrintName return void?
Good question. There is no requirement to capture a return value. Just because a function returns a value doesn't mean that the calling code needs to do anything with it. For many functions, like ...
1
vote
Difference between Pass by Value and Pass by Reference (a practical example)
For set_int() function, it's not complicated. You understand it correctly. However, things are confusing for C arrays. Actually, to me, accessing array member via bracket notation is just a syntactic ...
Only top scored, non community-wiki answers of a minimum length are eligible