0

When I run check50 on this code it says it wont work, but it seems to look fine when I just run the script. It says "expected "1" character 3 of line 4, not "2" " Can you spot what I'm doing wrong? Thanks!

void init(void)
{
    int dsquared = (d * d) - 1;
    int i = 0, j = 0;
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            board[i][j] = dsquared--;
        }
    }
    if (dsquared % 2 == false)
    {
        int tmpswap = board[i-1][j-2];
        board[i-1][j-2] = board[i-1][j-3];
        board[i-1][j-3] = tmpswap;
    }
}
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 loop setups. This is called creating shadow variables. The shadow vars take precedence while they are in scope, hiding the original vars. When the loops finish and the shadow vars cease to exist, the original vars are exposed again, but they are still 0. The result is that the code isn't swapping the values that you want.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thanks! I figured out too that because I wasn't returning during that if statement that it wasnt passing it along. thanks again!
    – Michael
    Apr 12 '16 at 20:44

You must log in to answer this question.

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