My output is supposed to be four triangles and a pyramid. I managed to get the four triangles but can't figure out the pyramid. Any help would be great. (I also have to use setw and setfill).
The output is a left aligned triangle, then left aligned upside down. right aligned triangle, then right aligned triangle upside down.
This is my current output:
#include <iostream>
#include <iomanip>
using namespace std;
//setw(length)
//setfill(char)
int height; //Number of height.
int i;
int main()
{
cout << "Enter height: ";
cin >> height;
//upside down triangle
for (int i=height; i>=1; i--){ //Start with given height and decrement until 1
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n"; //line break between
//rightside up triangle
for (int i=1; i<=height; i++){ //Start with 1 and increment until given height
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n";
//right aligned triangle
for (int i=1; i<=height; i++){ //Start with 1 and increment until given height
cout << setfill (' ') << setw(i-height) << " ";
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n";
//upside down/ right aligned triangle
for (int i=height; i>=1; i--){ //Start with given height and decrement until 1
cout << setfill (' ') << setw(height-i+1) << " ";
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
cout<< "\n";
//PYRAMID
for (int i=1; i<=height; i++){ //Start with 1 and increment until given height
cout << setfill (' ') << setw(height-i*3) << " "; //last " " is space between
cout << setfill ('*') << setw((i)) <<"*";
cout << "\n";
}
}//end of main
The call to setfill('*')
will overrule the call to setfill(' ')
on the previous line when you are drawing the pyramid. There can be only one fill character set per line.
You could try "drawing" the asterisks by "hand", like this:
for (int i = 1; i <= height; i++) {
cout << setfill (' ') << setw(height - ((i - 1) * 2 + 1) / 2);
for (int j = 0; j < (i - 1) * 2 + 1; j++)
cout << '*';
cout << "\n";
}