C++ triangle pattern

Hi, I would like some help with this problem.
I have to create a program that will create this pattern:
http://gyazo.com/124b7f7235749e8cdf0367eb29483000.png

Here is what I have so far:

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>

#include <cstdlib>
using namespace std;

int main()
{
cout<<"\nEnter the no.: \n";
int i,j,n;
cin>>n;
for(i=1;i<=n;i++)
  {
   j=i;
   for(;j;j--)
     cout<<"*";
     
  j=n;
   for(;j;j--)
     cout<<" ";  
  
     
   cout<<endl;
  }
    }
*            ************ ************            *
**           ***********   ***********           **
***          **********     **********          ***
****         *********       *********         ****
*****        ********         ********        *****
******       *******           *******       ******
*******      ******             ******      *******
********     *****               *****     ********
*********    ****                 ****    *********
**********   ***                   ***   **********
***********  **                     **  ***********
************ *                       * ************


What you have so far generates something like this:
*.....
**.....
***.....
****.....
*****.....

For the sake of clarity, I replaced space by a dot.

Try this:
1
2
3
4
5
6
7
8
9
10
11
    for (i=1; i<=n; i++)
    {
        // 1a
        for(j=i; j; j--)
            cout<<"*";
        // 1b
        for(j=n-i+1; j; j--)
            cout<<".";

        cout<<endl;
    }


I labelled the code 1a and 1b, as this is the first of four main sections of the image.

Concentrate now on part 2a and 2b.

After that, the second half is a mirror-image of the first, so the code can be more or less cut and pasted for that.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.