Hot answers tagged

5

Recall that arrays are zero based. So if you have an array of d elements, the index goes from 0 to d - 1.


2

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 that the array is a 2-d array with d elements in each dimension. More importantly, remember that the dimension indexes start at 0, not 1, so the largest valid ...


2

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 else, just do the swap after all of the loops. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)


2

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 same if statement block to print the underscores in draw(). If this answers your question, please click on the check mark to accept. Let's keep up on forum ...


2

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) { //BODY } Edit: For draw: Use %2d so that each time you get space for two letters and \t tab for equal spacing. printf("%2d\t", board[i][j]); and remove the ...


2

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() instead to remove all kinds of whitespace at both ends.


2

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 flag -Wshadow will warn in that case). Your single loop would output just the elements of the diagonale, needs nested loops just like when filling, and break ...


1

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 array. This draw function is drawing the same board every time, based on the calculated value arr_len. And it doesn't swap (not should it!).


1

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 that masks one with greater scope, such as a var outside a currently active loop or a global variable. When the code exits init(), the local board[][] array is ...


1

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 cannot access the global one because it has the same name as the local variable), and all the numbers you write to the local array die with leaving the scope it'...


1

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 blank tile and the tile to move without altering anything along the way. [EDIT: following code update] As it turns out, a larger problem is lurking in the code ...


1

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".


1

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> #include <string.h> #include <ctype.h> #include <cs50.h> int main(int argc, char* argv[]) { int d = atoi(argv[1]); printf("d = %i\n"...


1

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. (Take a look at the source code for main. ) It makes a first check by looking at the file size to see that it is correct. The designers expected 0 (a single ...


1

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 dsquared initially, it is 0 by the time you do the test. Once that's fixed, you've declared i and j before the for loops, and then redeclared them in the for ...


1

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 your second set of nested for loops, which brings me to the second issue... The last line of your code board[d-1][d-1] = -1; is unnecessary. You want to keep board[...


1

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 board[MAX][MAX] has already been declared as a global variable in the distributed code.


1

Just figured it out! My if statement was nested incorrectly. It should not be in the inner for loop!


1

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 int array, you actually set the value to 95, the ASCII value of an underscore. Since check50 is expecting 0 but got 95, it got an extra digit, thus the error. ...


1

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 feel right, try writing by hand the expected values generated when making a small board, say the smallest value for d as possible and increase it by 1 or 2 if ...


1

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 spec with the correct link to the source code? Here is fifteen.c. Be sure that's the version you are using. Based on what you say your log.txt shows, you are ...


1

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 result. We start with a control variable before for loop, that contains the desired value when the loop (i = 0, j = 0) starts this value can be k = d*d-1 Like ...


1

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 original source code file? You should only be modifying the sections with the //TODO comments. Do not remove any of the other code unless specifically permitted ...


1

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 initialized to a valid value. int z = (d*d) - 1; Nor is housed in the right place, at each iteration of i (suppose the loop has three iterations) you do z = 8, in ...


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