How to print out this pattern

Hi!

The pattern I need to print out looks like the right and the left side of a triangle combined.
I can print out the right and the left side of a triangle separated but not on the same level. this is how I do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
\\ the pattern should look like this:
   *   *
   ** **
   *****
  #include<iostream>
using namespace std;
main()
{
    for(int row = 0; row < 3; row++)
       {
            for(int col = 0; col <= row; col++)
                cout << '*';
                cout << endl;
       }
    for(int row = 3; row > 0; row--)
      {
            for(int space = 0; space < row - 1; space++)
                cout << ' ';
            for(int star = 0; star < 3 - row + 1; star++)
                cout << '*';
                cout << endl;
       }
}
Last edited on
One outer loop over the rows. Inner loop over the columns of one row.

What to do for the row? This is not quite correct:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using std::cout;

int main()
{
    for( int row = 0; row < 4; ++row )
    {
        for ( int col = 0; col <= row; ++col ) {
            cout << '*';
        }
        for ( int space = 0; space < 4 - 2*row; ++space ) {
            cout << ' ';
        }
        for ( int col = 0; col <= row; ++col ) {
            cout << '*';
        }
        cout << '\n';
    }
    return 0;
}

In fact, I would rather have only one inner loop and calculate a boolean from (row,col). Hint: abs()
Topic archived. No new replies allowed.