Hot answers tagged

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.
user avatar
  • 2,625
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 ...
user avatar
  • 64.2k
2 votes

Pset3/Fifteen/Init Function - Problem with 1 and 2 swap

Actually, the swap shouldn't be inside any for loop. You only want to do the swap once. Putting it inside any loop does it multiple times. Since the swap only depends on the value of d, and nothing ...
user avatar
  • 64.2k
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 ...
user avatar
  • 64.2k
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) { //...
user avatar
  • 549
2 votes
Accepted

PSET6 2016 Analyzer.py - Says all is neutral

In analyze, shouldn't you use self.positives, and not open the files again? Now to the real problems: .lstrip(" ") does not remove the line break at the end of the line, you could use .strip() ...
user avatar
  • 20.7k
2 votes
Accepted

Pset3 - Game of Fifteen - init function

You use local stack variable for the board, local variables will be deleted on leaving the function, should probably use a global variable. Local variables of same name shadow global ones (compiler ...
user avatar
  • 20.7k
1 vote
Accepted

pset3 - swap in function Init

The swapping looks ok (at least in the first init function). The thing that is "not giving the expected results" is the draw function. The draw function should print the "current state" of the board ...
user avatar
1 vote
Accepted

game of fifteen problem with initialisation of my 2D array

board[][] is a global variable. When you redeclared it inside of the init() function, you created what is called a shadow variable. A shadow variable is a variable that is created with local scope ...
user avatar
  • 64.2k
1 vote
Accepted

Game of Fifteen Draw function outputting only zeros

In init, do not declare board using int board[d][d];. This variable is already declared globally, so that all functions can access it. By declaring your own variable, you shadow the global one (you ...
user avatar
  • 20.7k
1 vote

The tile doesnt swap or move when i use this code!

Why is this line, board[r][c]=t-1; in the move() function? It will reassign values to every tile on the board. It looks like a remnant of a cut and paste from init(). The code needs to find the ...
user avatar
  • 64.2k
1 vote

CS50 > Pset3 > Game of fifteen > init function :

If they are not swapped, the puzzle is not solvable. You can review the Wikipedia article about the game to learn about the arcane mathematics "happening behind the screen".
user avatar
1 vote
Accepted

What's the difference between these two lines of code? Pset3. Init

Both appear to work. I ran the following test program and it works fine, printing out for both when d=4. Is it possible that you have some other difference in code somewhere? #include <stdio.h>...
user avatar
  • 64.2k
1 vote

pset3 Game of Fifteen check50

This problem usually occurs for one of two reasons. It looks like you've hit both. The check50 program looks at the log file generated in main(), not the actual screen output to check the program. (...
user avatar
  • 64.2k
1 vote
Accepted

check 50 says 4x4 wont initialize properly

The issue is that the code isn't swapping the 1 and 2 tiles when it should for the initialization. Two problems. First, dsquared is decremented to 0 in the for loop, so no matter what value is in ...
user avatar
  • 64.2k
1 vote
Accepted

PSET3 check 50 error on init()

I see two issues, both of which are related. Your first set of nested for loops repeatedly sets board[i][j] = 0;. You don't need to do this. You will end up setting board[i][j] to 0 at the end of ...
user avatar
  • 1,424
1 vote

How to set up the board using init and draw functions

In your init function you need to assign values to each position in the board array. In the draw function, you use the values from your board array to print to the screen. Note that the integer array ...
user avatar
1 vote
Accepted

Pset3/Fifteen/Init Function - Problem with 1 and 2 swap

Just figured it out! My if statement was nested incorrectly. It should not be in the inner for loop!
user avatar
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 ...
user avatar
  • 64.2k
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 ...
user avatar
  • 167
1 vote

I am facing problem in init function. It shows up same error everytime that incorrect length 26, should be 18. I am using latest version of cs50

Your log should look like this when you first run the program: 15|14|13|12 11|10|9|8 7|6|5|4 3|1|2|0 Did you modify the code that writes to the log? Are you sure you are using the 2016 edX pset3 ...
user avatar
  • 18.4k
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 ...
user avatar
  • 5,172
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 ...
user avatar
  • 64.2k
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 ...
user avatar
  • 5,172

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