0

I cannot figure out why this code is not working. The output appears to be random. Any help is greatly appreciated. Thanks! My Init code:

void init(void)
{
    int MAX = (d*d)-1;
    int below = 0;
    int board [d][d];

    for (int i=0; i<d; i++)
    {    
        for (int j=0; j<d; j++)
        {
            board[i][j] = MAX-below;
            below++;
        }
    }
    //if  even switch 1 and 2
    if (d/2 == 0)
    {
        int gridtwo;
        gridtwo = board [d-(d-2)][d-(d-2)];
        board [d-(d-2)][d-(d-2)] = board [d-(d-1)][d-(d-1)];
        board [d-(d-1)][d-(d-1)] = gridtwo;
    }
}

My draw code:

void draw(void)
{
    int board [d][d];

    for (int i = 0; i<d; i++)
    {
        for (int j = 0; j<d; j++)
        {
            if (board[i][j]==0)
                printf("_   ");
            else
            {
                printf("%d", board[i][j]);
                printf("   ");
            }
        }
        printf("\n");
}

Thanks again!

  • why do you have board[][] variable again? I remember from this assignment, it is already declared as a global variable. you should not Change it. use that one. – Wafeeq Jul 11 '16 at 16:37
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 printf below. your complete draw function should look like

for (int i = 0; i<d; i++)
{
    for (int j = 0; j<d; j++)
    {
        if (board[i][j]==0)
            printf(" _\t");
        else
        {
            printf("%2d\t", board[i][j]);

        }
    }
    printf("\n");
}

Also your swap code is wrong. You want to replace the second element from the last row not from the second last row. Last row will be d-(d-1)

int gridtwo;
gridtwo = board [d-1][d-3];
board [d-1][d-3] = board [d- 1][d-2];
board [d-1][d-2] = gridtwo;

Note: accept the answer if your Problem is solved.

| improve this answer | |
  • Thanks, I fixed that part of my code, but am still having random number outputted. Any other suggestions? Thanks! – Iris Frenzy Jul 11 '16 at 17:29
  • Can you post your Output? how does it Looks like? – Wafeeq Jul 11 '16 at 17:43
  • I guess now the Problem should be solved. check the edit in my answer. – Wafeeq Jul 11 '16 at 17:49
  • Thank you so much! – Iris Frenzy Jul 11 '16 at 19:15
0

The test for even is incorrect and it is switching two fixed tiles. Dividing by two doesn't tell if the number is even. It needs to be a modulo % operation.

The code is switching a fixed pair of tiles in the board[][] array. board [d-(d-2)][d-(d-2)] is always going to be board[2][2]. Similarly, board [d-(d-1)][d-(d-1)] will always be baord[1][1].

If you simplify the calculation, it looks like this: d-(d-2) = d + ( -1 * (d-2) ) = d + ( -d + 2 ) = d - d + 2 = 2

So, you're not swapping the 1 and 2 tiles, you're just swapping two fixed tiles no matter how large the array is.

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

| improve this answer | |
  • Thank you, I fixed that part of my code, but am still getting random numbers printed out. Any other suggestions? – Iris Frenzy Jul 11 '16 at 17:29

You must log in to answer this question.

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