Pattern

I am really confused on how to to this pattern. (this must be the output with cin)

________1
______1 2
____1 2 3
__1 2 3 4
1 2 3 4 5

(the underscore represents spaces)
Last edited on
for each line
  you have to show X spaces
  and Y numbers

"for each" sounds like a loop, does it not?

Okay, a loop that repeats as many times as there are lines.
The loop's counter has "number of current line"?

What to do for one line?

How to "show X spaces"? With a loop:
loop X times
  show space

How to "show Y numbers"? With a loop:
loop Y times
  show number

Newline.


You can compute X and Y from the number of current line.
Last edited on
Thanks! Your explanation is really a big help. What if I need the user to input the number of rows?

#include <iostream>
using namespace std;

int main()
{
int rows;

cout << "Enter number of rows: ";
cin >> rows;

for(int i = rows; i >= 1; --i)
{
for(int j = 1; j <= i; ++j)
{
cout << j << " ";
}
cout << endl;
}

return 0;
}
Does it really matter how many rows there are, if your math is solid?
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
  int rows;
  std::cin >> rows;
  for ( int i = rows; i >= 1; --i )
  {
    std::cout << i << ' ' << rows - i << '\n';
  }
}
Topic archived. No new replies allowed.