for triangle

I want to put the square in between the triangles to create a star, I don't know where to put my code of the square. Thank you!

output:
*
***
*****
*******
*********
* *
* * * *
* * * * * *
* * * * * * * *
* * * * ** * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
*******
*****
***
*
code:
#include<iostream>
using namespace std;
int main()
{
int i, j, rowNumber, space;
cout<<"Enter the Number of Rows: ";
cin>>rowNumber;
space = rowNumber-1;
for(i=1; i<=rowNumber; i++)
{
for(j=1; j<=space; j++)
cout<<" ";
space--;
for(j=1; j<=(2*i-1); j++)
cout<<"*";
cout<<endl;
}
for(int i=1; i<=5; i++)
{
for(int k=1; k<=5-i; k++)
{
cout<<" ";
}

for(int j=1; j<=i; j++)
{
cout<<" *";
}

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

for(int row=1; row<=i;row++)
{
cout<<"* ";
}
cout<<endl;
}

for(int i=1; i<=5; i++)
{
for(int j=1; j<=5; j++)
cout<<"* ";
cout<<endl;
}



space = 1;
for(i=1; i<=(rowNumber-1); i++)
{
for(j=1; j<=space; j++)
cout<<" ";
space++;
for(j=1; j<=(2*(rowNumber-i)-1); j++)
cout<<"*";
cout<<endl;
}
cout<<endl;
return 0;
}
These kinds of homework work best when you get yourself some graph paper and a pencil and draw what you want your final shape to look like.

Then draw a vertical line along the left side of the object so that you can count empty squares and see how to write the loops to make things work.

The trick is that you aren't just printing asterisks... you are also printing spaces.
When you can count both spaces and asterisks you can see the relationship between them and the loop counter.

Good luck!
Topic archived. No new replies allowed.