26
votes
How to solve "control may reach end of non void function" error
This problem comes from a misunderstanding of the flow of control in your algorithm. Here's a representation of that control flow in pseudocode:
if (outer-condition-1)
if (inner-condition-1)
...
8
votes
Where can I find 3x3.txt?
First, run update50 to make sure you are up to date. Run appliance50 to verify that you have version 19-2.
Then, run ls ~cs50/pset3
You should see the files there.
As a last resort, download ...
7
votes
Accepted
Pset3 won-function works only with 3x3 board. Where am I wrong?
As correctly pointed out by Kareem it is quite confusing when you start to debug the long 'if' condition and it may also at times give you wrong answer.
I would suggest an alternative approach to ...
5
votes
Why Aren't My Last Two Tiles Swapping?
Recall that arrays are zero based. So if you have an array of d elements, the index goes from 0 to d - 1.
5
votes
Accepted
How do I fix a "use of undeclared identifier" compilation error?
The problem is the semicolon on line 138:
for (int column = 0; column < d - 1; column++);
In C, a semicolon is used to terminate a statement. The body of the for loop (the part in curly braces) ...
4
votes
Pset3 won-function works only with 3x3 board. Where am I wrong?
Following your code, there are some issues.
First, it's not a good practice to have such looong line of code. It's recommended by the CS50 staff that your line shouldn't exceed 80 characters.
Second,...
4
votes
Accepted
Fifteen move not working in last row only
A very common problem. Look at the following:
else if (i < d && board[i + 1][j] == 0)
This code attempts to swap with a tile that is off the edge of the board. If i = d-1, then what is ...
3
votes
Pset3 Fifteen Move function
If you keep track of the location of the blank tile, you can check based on its location and the coordinates of the number to be moved if they're swappable, at which point you just have to set the ...
3
votes
Accepted
Pset3 Fifteen Move function
You have a multidimensional array of size [x][y] (I know that in this case x = y... it's just to make it more clear), so, you need to look for the tile you want to swap simply going through the array:
...
3
votes
Accepted
How to Print the Blank Tile in the Game of Fifteen
You may use an if/else statement like
if the current value is 0
// do something
else
// do something else
3
votes
Blank tile without using a variable
Welcome to SE, first of all. Now, for your problem, think that when you had made the draw function for the GoF, there you were printing the values of the tile using printf, so using that very printf's ...
3
votes
pset 3 "control may reach the end of a non-void function"
Your function is declared as having a return type, but the structure of your function is not guaranteed to return anything.
For example, if you have:
bool myfunction(int x)
{
if(x > 10)
{...
3
votes
Accepted
move function, pset3
Well, I don't know how to tell you this, but I plugged your move function into my code and it worked perfectly. I suspect that you have a problem elsewhere in your code. Have you altered something in ...
3
votes
Accepted
Pset 3: Fifteen - Moving up and down but not left or right?
So, it was a subtle problem and an easy mistake to make. Look at the limits on your 4 conditions: > 0 and < d. >0 works because it allows for swapping to array elements with 0 as an index. ...
3
votes
Accepted
PSET 3: Game of fifteen. Everything in the board is equal to zero
You only need to delete this and the code will work:
int board[d][d];
The reason is that the global variable board is declared above and can be used by any function
int board[DIM_MAX][DIM_MAX];
...
3
votes
A wierd error in won function (fifteen)
the hint is in
1-2-3|4-5-6|0-7-8
ie
1 2 3
4 5 6
0 7 8
Your won function will say that's a winning position, which it isn't.
That same sort of scenario doesn't occur during the 4x4 gameplay.
To ...
2
votes
How to pass grid values to draw in pset3
The board was already declared for you as a global array, so you should be setting the initial values in your init() and then drawing that array in your draw(). The draw() function is also called ...
2
votes
Accepted
Swap 2 and 1 after looping, pset3, Game of Fifteen
Don't you want to swap the two tiles? You are swapping 2 new integers that happen to hold the tile values.
Get rid of those 2 extra integers and simply use the tiles themselves in your swap code.
...
2
votes
Global variables to store the position of the blank space?
Declare hori and verti as global variables and initialize them to d - 1 just after init().
2
votes
What is if(!move(tile)) and can I change it?
if (!move(tile)) is a shortcut for
bool didItMove = move(tile);
if (didItMove == false)
{
// do this
}
In other words, it first calls the move function (that you are writing), passing in the tile ...
2
votes
Accepted
All my moves entered are shown illegal, though code doesn't seem wrong... (PS3 - fifteen)
The thing is:
The program has declared a global variable d already.
//board dimension
int d;
In your program, there's another declaration of d, in your a b c d initialisation.
Thus, your for ...
2
votes
Accepted
pset 3 - game of fifteen - move tile
It looks like you didn't account for the corner cases where the blank is on the edge of the board. In other words, when you try to swap, i or j could go out of range, essentially swapping with ...
2
votes
Accepted
pset3 fifteen's draw function problem
I just tested your code and got it working with a couple minor tweaks. My advice is to go look back at your global variables. Something you have in both those functions has been declared earlier in ...
2
votes
Accepted
Pset3: strange problem while testing init() function
When you declare an array, such as board, you want to use the actual size of the array, without subtracting 1.
2
votes
Accepted
pset3 fifteen - how to check if tile is adjacent to 0/"_"?
Simply put, if tile is in board[i][j], you need to check the positions directly above, below, and on either side for 0. In other words, board[i+1][j], board[i-1][j], board[i][j+1], board[i][j-1].
But ...
2
votes
Accepted
pset3 fifteen adjacent check not working
It fails because 3 out of 4 of your if conditions are wrong.
2
votes
Accepted
Pset3 Fifteen Check50 not Working
Wow, this is something I've never seen before. Congratulations! ;-) Your code is both right and wrong at the same time! :-D
Yes, your code displays correctly and looks perfectly correct. It also ...
2
votes
Accepted
Pset3 Fifteen: Draw and/or Init Function Problem
You cannot check if number is even with if (d/2 == 0)
use % sign (remainder sign).
Number is even if you divide the number with 2 and remainder becomes 0.
for example
if (d%2 == 0)
{
//...
2
votes
Accepted
Am I going about Init() correctly?
It looks good (not a guarantee) right up until the end. What do you think will happen when you try to assign a string to an int?
Instead, leave the 0 in place for the blank tile here, and use the ...
2
votes
Accepted
Error in dependency - pset3 fifteen
Assuming it does compile, the move function has a couple of serious flaws. First, given the math used, the tests should be comparing to d, not d-1. This error is preventing anything on the right side ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
game-of-fifteen × 205pset3 × 167
move × 40
fifteen × 34
won × 25
check50 × 14
init × 11
c × 10
illegalmove × 10
cs50x × 9
draw × 9
fifteen.c × 7
error × 5
pset3-fifteen × 3
cs50-ide × 2
cs50x2017 × 2
gdb × 2
swap × 2
game × 2
multi-dimensional-arrays × 2
pset4 × 1
array × 1
fails-check50-but-works × 1
function × 1
cs50submit × 1