0

I'm working on my 'draw' function. I'm managing to print the array yet for some reason, it's printing it in one long line as opposed to a grid.

Here is my code:

void init(void)
{
// initilizes array

int total = d * d; 

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

// Swap 2 and 1 if even number of spaces

if(total % 2 == 0)
{
    int temp = board[d-1][d-2];
    board[d-1][d-2] = board[d-1][d-3];
    board[d-1][d-3] = temp;
   }
}

void draw(void)
{
// TODO
for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
        if(board[i][j] == 0)
        {
            printf(" ");
        }
        else
        {
            printf("%2d  ", board[i][j]);

        }
        printf("\n");
    }
}

Any idea? I would've thought that after I've the iterated through inner loop of 'draw' and added new line that it would print out correctly.

Instead, I just get them printed in one long column.

Any suggestions please?

1

It is printing the line feed after each tile. It should only print after each row. Maybe you have placed the printf(\n) in the wrong place? What in the code defines the end of the row? What step occurs at the end of the row?

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

2
  • hmmm I see, well I tried adding taking out the printing of the new line and saying: if(i++) { printf("\n"); } But then I just get: 15 14 13 12 7 6 5 4 My logic was that whenever the outside loop completes an iteration, that would mean that 0,0 0,1 and 0,2 would have been printed, so then I'd need a new line for 1,0, 1,1 and 1,2. Clearly I was wrong :S – gloopit Jul 7 '16 at 18:07
  • Fixed, no worries :) – gloopit Jul 7 '16 at 20:04

You must log in to answer this question.

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