0

When I paste my code into Python Tutor, I get the result I expect: view my code here.

However, the code doesn't work in the CS50 IDE. An array of the correct dimensions is generated, but it is populated by zeroes. I've tried debugging with debug50 and gdb, but I'm having trouble finding out what the issue is. Here is my code:

/**
* Initializes the game's board with tiles numbered 1 through d*d - 1
* (i.e., fills 2D array with values but does not actually print them).  
*/
void init(void)
{
    int board[d][d];
    int counter = (d * d) - 1;
    for (int i = 0; i < d; i++) {
        for (int j = 0; j < d; j++) {
            board[i][j] = counter;
            counter--;
        }
    }
}

/**
 * Prints the board in its current state.
 */
void draw(void)
{
    for (int i = 0; i < d; i++) {
        for (int j = 0; j < d; j++) {
            printf("%i", board[i][j]);
        }
        printf("\n");
    }
}
1

Remove this line of code from your init function: int board[d][d];

The global variable for the board is declared at the top of the code already (around line 30). You are supposed to use that, so you are able to modify/access it in each function.

You will soon learn about pointers in the class, which provide another way in C of passing around values in order to allow functions to modify them. But for now, work with what the distro code gives you. See if that resolves your issue.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .