0

I am using the cs50 2015 code template and my code gives the correct output on my system but gives a check50 error

:) fifteen.c exists

:) fifteen.c compiles

:( init initializes 3x3 board correctly

\ expected file to be of length 18, not 19

:( init initializes 4x4 board correctly

\ expected file to be of length 38, not 39

and so on

Here is my code

int val = d*d - 1;
    for (int i = 0; i < DIM_MAX; i++)
        for(int j = 0; j < DIM_MAX; j++)
            board[i][j] = 0;

    for (int i = 0; i < d; i ++ )
    {
        for(int j = 0; j < d; j ++)
        {       
                board[i][j] = val --;
        }
    }
    /** If dim board = even swap positions of 1 and 2 */   
    if (d % 2 == 0)
    {   
        board[d-1][d-3] = 1;
        board[d-1][d-2] = 2;
    }
    board[d-1][d-1] = -1;

Please Help!!

1

I see two issues, both of which are related.

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

  2. The last line of your code board[d-1][d-1] = -1; is unnecessary. You want to keep board[i][j] as 0 for a variety of reasons.

Ultimately, you have correct code surrounded by extraneous code that is impacting what would otherwise work just fine.

1
  • Thanks Peter :) . – user2300144 Feb 20 '16 at 5:52

You must log in to answer this question.

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