C++: Numbering lines in a for loop

Hi everyone,

I have a question about numbering lines in a for loop.
To demonstrate what I mean, here is a program that prints a right triangle made of numbers, and it marks each line with a number:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{

int x, i, j;
cout << "enter number of row desired: ";
cin >> x;

for (i=1; i<=x; i++)
{
    cout << "row " << i << ": ";
    
    for(j=1; j<=i; j++)
    {
        cout << j << " ";
    }
    cout << endl;
}

return 0;
}


Assume the user enter 10, then the output will be:

1
2
3
4
5
6
7
8
9
10
row 1: 1                                                                                                                          
row 2: 1 2                                                                                                                        
row 3: 1 2 3                                                                                                                      
row 4: 1 2 3 4                                                                                                                    
row 5: 1 2 3 4 5                                                                                                                  
row 6: 1 2 3 4 5 6                                                                                                                
row 7: 1 2 3 4 5 6 7                                                                                                              
row 8: 1 2 3 4 5 6 7 8                                                                                                            
row 9: 1 2 3 4 5 6 7 8 9                                                                                                          
row 10: 1 2 3 4 5 6 7 8 9 10 



Now, my question is as follows. It easy to number these lines since "i" increments with each iteration. However, what about when I want to draw an INVERTED right triangle. How can I number it then?

The code for the inverted one is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{

int x, i, j;
cout << "enter number of row desired: ";
cin >> x;

        for (i=x; i>=1; i--)
        {
            
            for(j=1; j<=i; j++)
            {
                cout << j << " ";
                
            }
            cout << endl;
            
        }
        
return 0;
}


I tried many things including loops within loops but I just can not get it to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
# include <iostream>

int main() {
  int n_rows = 0; std::cin >> n_rows;

  for (int row = n_rows; row >= 1; --row) {
    std::cout << "row " << (n_rows - row) + 1 << ": ";            

    for (int i = 1; i <= row; ++i)
      std::cout << i << ' ';
    std::cout << '\n';
  }
}

Last edited on
oh man... i couldn't have guessed its this straight forwards..
Thank you so much.
Topic archived. No new replies allowed.