简体   繁体   中英

Nested for-loops with numbers going down columns

So, obvious newbie here... My goal is to output a table with eight columns and ten rows with the numbers 1 through 80. This must be done using nested for loops (for an assignment). This is what I have so far:

int num = 1; //start table of numbers at one

for (int row = 0; row < 10; row++) //creates 10 rows
{
    for (int col = 0; col < 8; col++) //creates 8 columns
    {
        cout << num << "\t" ; //print each number
        num = num + 10;
    }

    cout << num;
    num++;

    cout << endl; //output new line at the end of each row
}

But my output should look like this:

1 11 21 31 41 51 61 71
2 12 22 32 42 52 62 72
...
10 20 30 40 50 60 70 80

Am I even in the right direction? How do I do this? Right now, only the first row is correct when I print it.

We beginners should help each other. Try the following

#include <iostream>
#include <iomanip>

int main()
{
    const int ROWS = 10;
    const int COLS = 8;

    for ( int row = 0; row < ROWS; row++ )
    {
        for ( int col = 0; col < COLS; col++ )
        {  
            std::cout << std::setw( 2 ) << row + 1 + ROWS * col << ' ';
        }
        std::cout << std::endl;
    }        

    return 0;
}

The output is

 1 11 21 31 41 51 61 71 
 2 12 22 32 42 52 62 72 
 3 13 23 33 43 53 63 73 
 4 14 24 34 44 54 64 74 
 5 15 25 35 45 55 65 75 
 6 16 26 36 46 56 66 76 
 7 17 27 37 47 57 67 77 
 8 18 28 38 48 58 68 78 
 9 19 29 39 49 59 69 79 
10 20 30 40 50 60 70 80 

You are in the right direction except the variable num is being changed incorrectly.:) For example after the first iteration of the outer loop num is equal to (if I am not mistaken) 72 .

Here is a more general approach that allows to set the initial value.

#include <iostream>
#include <iomanip>

int main()
{
    const int ROWS = 10;
    const int COLS = 9;
    const int INITIAL_VALUE = 10;

    for ( int row = 0; row < ROWS; row++ )
    {
        for ( int col = 0; col < COLS; col++ )
        {  
            std::cout << std::setw( 2 ) << INITIAL_VALUE + row + ROWS * col << ' ';
        }
        std::cout << std::endl;
    }        

    return 0;
}

The program output is

10 20 30 40 50 60 70 80 90 
11 21 31 41 51 61 71 81 91 
12 22 32 42 52 62 72 82 92 
13 23 33 43 53 63 73 83 93 
14 24 34 44 54 64 74 84 94 
15 25 35 45 55 65 75 85 95 
16 26 36 46 56 66 76 86 96 
17 27 37 47 57 67 77 87 97 
18 28 38 48 58 68 78 88 98 
19 29 39 49 59 69 79 89 99 

All you need to do is:

int num = 1; //start table of numbers at one

for (int row = 0; row < 10; row++) //creates 10 rows
{
    for (int col = 0; col < 8; col++) //creates 8 columns
    {
         cout << num << "\t" ; //print each number
         num += 10;
    }
num = row + 2;
cout << endl; //output new line at the end of each row
}

EDIT: FIXED

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