# Pset3. Specifically, what does this code do? Initializing array

Can you please tell me what this code does specifically? How does it work?

void init(void) {

``````int total = d * d;

for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
board[i][j] = --total;
}
}
``````

}

I'm really struggling with Game of Fifteen.

I just don't know where to start so I found the first part of a solution and have decided to make sure I know it in and out before moving on. I need to understand it as opposed to just copying it.

I don't understand how this works though.

I'm assuming this populates the array with variables, but I'm not sure how. On the first pass through the for loop, both i and j will be 0 right? So the first value will be: board which if --total (assuming total is 9), it will be value no. 8?

But then on the second pass through, if both i and j are incremented by 1, then the next array value is board, as opposed to  or even  right? I'm really confused, please walk me through this code!

First `--total`. The two minus signs that precede it mean to decrement it by 1 before processing the rest of the line. If the two minus signs had followed it instead, it would mean to process the line with the current value of the variable and then decrement. So, that means that the value used is 8, not 9.
• no, the inner loop increments from 0 to 1 to 2 and exits. When it exits the inner loop, the outer loop increments once and the inner loop starts over, 0,1,2,exit, and so on. Put the following statement inside your inner loop following the board[][] assignment, compile and run: `printf("i=%i, j=%i, total = %i\n", i, j, total);` Jul 6, 2016 at 16:34