Hot answers tagged

4 votes

"Expected expression" error in my Game of Fifteen code

You are using a comparison symbol that does not exist. Change !== with != and that should do the work for the while statement. About the else I can't really help without some more information... Did ...
3 votes
Accepted

Void init(void) or void init(int d)?

The int variable named d is a global variable. Global variables are variables that are declared outside of any functions. They can be accessed from anywhere in the source code file and that's why we ...
  • 17.5k
3 votes
Accepted

Game of Fifteen - Move function issue

You have two serious problems here. First, your move() function defines int blank = board[d][d]; The array element board[d][d] doesn't exist because both indexes run from 0 to d-1. Anything with d ...
  • 65.4k
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]; ...
  • 1,139
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 ...
  • 1,424
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 ...
  • 36
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 ...
  • 65.4k
2 votes
Accepted

PSET3 fifteen.c - no idea how to adjust spaces for game board

"Give a man a fish and you feed him for a day. Teach him to fish and you feed him for a lifetime." You are currently printing out the tile values with %i as the placeholder for the tile in your ...
  • 65.4k
2 votes
Accepted

pset3: "fifteen" - should "draw" include "move"?

The reason that it only draws the initialized board is that move() is not swapping any tiles. The code is setting several int vars equal to values in the board[][] array, effectively making copies of ...
  • 65.4k
2 votes
Accepted

Why is the code after the for loop not executed?

It probably is executing, but not the way you think. You're lucky it isn't seg faulting. (Well, actually, a seg fault would be better. ) Think about the array indexes that you're using. Remember ...
  • 65.4k
2 votes

pset3 fifteen won

You are checking if everything is alright. And if the first part is alright you are returning true right away. This piece of code is exiting if the second box is correct without checking the rest of ...
2 votes
Accepted

Error: function definition is not allowed here

Simply put, there is a missing closing curly brace, }, in the draw() function. It looks to be missing at the first if statement code block. Because of the mispairing/missing closing curly brace, it ...
  • 65.4k
2 votes

fifteen is outputting random numbers

Remember that you already got d from command line input, so there is no need for d=GetInt() in init() function. If on the other hand you are not providing d at command line, that would account for ...
  • 1,415
2 votes
Accepted

pset3: Help with Fifteen Move function

I think it's ok to search for both tile and empty tile, though you could maintain global variable(s) to store the position of the empty tile, some even keep track of the positions of all tiles, ...
  • 20.8k
2 votes

pset3 fifteen what am I doing wrong with my move function

Resolved! I was watching week 4s lecture and realised that my swap didn't make sense, I was expecting the program to switch two values at the same time. So I used a temp variable to store the value ...
  • 65
2 votes
Accepted

Problems with won function on pset3

You return true too early. You only check whether all the tiles are in the correct order, but you don't check the position of the empty tile. A quick fix would be to replace if (min == d*d) { ...
  • 20.8k
2 votes
Accepted

How to fix "variable-sized object may not be initialized" error?

It is not complaining about k (which is not a "variable sized object"). It is complaining about grid. The problem is because you are declaring grid here int grid[i][j] = k; by using the keyword *int*,...
2 votes

check50 failed on pset3 game of fifteen

check50 removes your draw function as that is not tested (you can personalize it however you'd like). Because of this, if you update any global variables in that function, they won't be updated when ...
  • 18.5k
2 votes
Accepted

Won()- Pset 3- Game of Fifteen

What happens at i == j == (d-1)? That's the last tile and it should equal 0, but your test will fail because of that. If that answers your question, please click on the check mark to accept. Let's ...
  • 65.4k
1 vote
Accepted

Fifteen.C Move Function Fails One Check50 Test

Look at the following: if ( board[row - 1][column] == 0 && row - 1 > 0 ) Why does row-1 have to be greater than 0? 0 is still valid. If this answers your question, please click on the ...
  • 65.4k
1 vote
Accepted

Fatal error: <cs50.h> file not included

Hmmm, two consecutive questions with roughly the same issue. After running check50 on almost every problem from pset1 to pset5, it only happened on a handful of programs. Also, it has stopped ...
  • 65.4k
1 vote

Check50 initialising error pset3

check50 has a lot of dependencies in order to work. One of them is that the blank tile is stored as 0 in the board[][] array. Your code appears to change that to -1, which takes up an extra ...
  • 65.4k
1 vote
Accepted

Pset3, Error in init function

It looks like the original code that you had was more correct than after the changes you made, whatever they were. The original problem lies here: board[d - 1][d - 1] = '_'; Since board[][] is an ...
  • 65.4k
1 vote

Pset3, Error in init function

The last element in your board does not need to be initialized by you. Remember you should organize your elements differently depending on the board size. The way you initialize your elements doesn't ...
  • 167
1 vote
Accepted

pset3: fifteen - problem with "move" function

The problem is with your if statements. In certain cases, you are trying to access array values that don't exist. Let's say the value at board[0][0] is 5. Will your first if statement be true? if (...
  • 44
1 vote
Accepted

pset3: fifteen - "init" and "draw"

8 7 6 5 3 1 0 -3 -6 It is what makes init () when d3, just have to take pen and paper and do some calculations. for(int i = 0; i < d; i++) { int k = i + 1; it leads to a wrong ...
  • 5,191
1 vote
Accepted

fifteen.c fails to initialize correctly

It looks like the code that creates and populates the log.txt file was removed from the source code. That code is absolutely critical for check50 to test your program. What have you removed from the ...
  • 65.4k
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 ...
  • 65.4k
1 vote

move in pset3 fifteen not working properly

This code will only change a tile adjacent to the target tile to 0. It won't actually swap the tiles. Once the code above finds the target tile in the board[][] array, it will choose one of the 4 ...
  • 65.4k
1 vote

init and draw not working properly in pset3 fifteen

your init function is incorrect as noted @Matt Obert. if (d%2 != 0) { for .... } if d = 4, for example, your function init will not doing nothing at all, since board [d-1] [d-2]; It not even ...
  • 5,191

Only top scored, non community-wiki answers of a minimum length are eligible