简体   繁体   中英

Nested Loop for C Programming

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.

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

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 if s

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 

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM