1

I have some problem about Nested Loop on C programming I tried to print the list like this:

 | |0|1|2|3|4|5|6|7|8|9|
 |0| | | | | | | | | | |
 |1| | | | | | | | | | |
 |2| | | | | | | | | | |
 |3| | | | | | | | | | |
 |4| | | | | | | | | | |
 |5| | | | | | | | | | |
 |6| | | | | | | | | | |
 |7| | | | | | | | | | |
 |8| | | | | | | | | | |
 |9| | | | | | | | | | |

but there are something problem when i type my code and display:

| |0|1|2|3|4|5|6|7|8|9|
|0|0|0|0|0|0|0|0|0|0|0|
|1|0|1|1|1|1|1|1|1|1|1|
|2|0|2|2|2|2|2|2|2|2|2|
|3|0|3|3|3|3|3|3|3|3|3|
|4|0|4|4|4|4|4|4|4|4|4|
|5|0|5|5|5|5|5|5|5|5|5|
|6|0|6|6|6|6|6|6|6|6|6|
|7|0|7|7|7|7|7|7|7|7|7|
|8|0|8|8|8|8|8|8|8|8|8|
|9|0|9|9|9|9|9|9|9|9|9|

There is my code :

void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player)
{
    int i, j;
    printf("| ");
    for (j = 0; j < BOARD_WIDTH; j++)
    {
        printf("|%d",j);
    }

    printf("|\n");

    for (i = 0; i < BOARD_HEIGHT; i++)
    {
        for (j = 0; j < BOARD_WIDTH; j++)
        {
            printf("|%d",i);
            if (j == 0)
            {
                printf("|%d",j);
            }
        }
        printf("|\n");
    }
    printf("\n");
}

Have someone can help for this condition: only one row and one column, other is empty.

3
  • 1
    Why is printf("|%d",i); inside the inner loop? Commented Mar 29, 2016 at 14:48
  • 1
    Perhaps printf("|%d",i); if (j == 0) --> if (j == 0) printf("|%d",i); Seems like printf("|%d",i) should be done outside the for (j) loop. Commented Mar 29, 2016 at 14:49
  • OH!!!!! I will fix that. However, it can't do one row and one column as well Commented Mar 29, 2016 at 15:00

5 Answers 5

1

At no point in the body of the inner loop are you printing spaces. You're instead printing the value of i, which is the column number.

        printf("|%d",i);
        if (j == 0)
        {
            printf("|%d",j);
        }

Instead, print i only on the first iteration and print the space each time:

        if (j == 0) {
            printf("|%d",i);
        }
        printf("| ");

Output:

| |0|1|2|3|4|5|6|7|8|
|0| | | | | | | | | |
|1| | | | | | | | | |
|2| | | | | | | | | |
|3| | | | | | | | | |
|4| | | | | | | | | |
|5| | | | | | | | | |
|6| | | | | | | | | |
|7| | | | | | | | | |
|8| | | | | | | | | |
Sign up to request clarification or add additional context in comments.

Comments

1

The key to getting this done correct is enclosing the repeating logic (the blank cells) in the loop while confining the specialized logic to be outside the loop:

void displayBoard(int height, int width)
{
    int i, j;

    printf("| ");
    for (j = 0; j < width; j++) {
        printf("|%d", j);
    }
    printf("|\n");

    for (i = 0; i < height; i++) {
        printf("|%d", i);
        for (j = 0; j < width; j++) {
            printf("| ");
        }
        printf("|\n");
    }
}

Look mom! no ifs

Comments

0

This is how you should code it:

void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player)
{
     int i, j;
    printf("| ");
    for (j = 0; j < BOARD_WIDTH; j++)
    {
        printf("|%d",j);
    }

    printf("|\n");

    for (i = 0; i < BOARD_HEIGHT; i++)
    {
        for (j = 0; j < BOARD_WIDTH; j++)
        {
            if (j == 0)
            {
                printf("|%d",i);
            }
            printf("| ");
        }
        printf("|\n");
    }
    printf("\n");
}

There are couple of issues with your current code:

  1. You printf("|%d",i); in the inner loop for every j while what you really want is just to print it when j == 0 once. The rests, you want to printf("| "):

    printf("| "); //you want to make this blank
    if (j == 0)
    {
        printf("|%d",i); //you want to print i here, not j
    }
    
  2. between the print of number when j == 0 and the print of blank should be reversed:

    if (j == 0)
    {
        printf("|%d",i); //you want to print i here, not j
    }
    printf("| "); //put the blank printing after the number
    

Comments

0

You are printing the number i in every line, when you want a blank or the array data. you also need to get rid of printing a zero every row, which is what you have coded in. Here is that part of the code corrected:

for (i = 0; i < BOARD_HEIGHT; i++)
    {
        for (j = 0; j < BOARD_WIDTH; j++)
        {

            if (j == 0)
            {
                printf("|%d",i);
            }
            printf("| "); ///or printf("|%d",board[i][j]) if you are after  stored data

        }
        printf("|\n");
    }

hope this helps.

Comments

0

Try this:

#define BOARD_HEIGHT 10
#define BOARD_WIDTH  10

void displayBoard()
{
    int i, j;

    printf("| ");
    for (j = 0; j < BOARD_WIDTH; j++)
    {
        printf("|%d", j);
    }
    printf("|\n");

    for (i = 0; i < BOARD_HEIGHT; i++)
    {
        printf("|%d", i);
        for (j = 1; j < BOARD_WIDTH; j++)
        {
            printf("| ");
        }
        printf("| |\n");
    }
}

BTW, there is no need to pass any arguments to this function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.