-2

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:

enter image description here

#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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ace
  • 59
  • 1
  • 7

2 Answers2

0

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"; 
}   
Steeve
  • 1,323
  • 1
  • 12
  • 23
0

It is always best to define the output you need before you start to think how to achieve it. Suppose you need a pyramid with height 5, as in your example. It means that the top row will have one *. In perfect world the second row will have two, but it is hard to achieve on the screen. Then maybe it can have 3. In this case the end result for height 5 will be: 1,3,5,7 and 9 *. (I tried to draw it here but wasn't successful, I suggest you draw it in any text editor to help visualize the end result).

Now regards the implementation: Note that what important is amount of filling blanks before *. The blank after will happen on its own. How much blanks should appear before *? If you try to draw the pyramid in a text editor you would realize that it depends on the width of bottom row and on the amount of * in each specific row. Also, if you look closely the blanks form a triangle...

Adding: Just to let you know - your original approach would also work, if you would choose to increase the number of * in each consequent row by 2 rather than one.

int BottomRowWidth = 1 + 2 * (height - 1);
int BlankNumber = (BottomRowWidth - 1) / 2;
int row, width;
for (row = 1, width =1; (row <= height); row++, width = width+2, BlankNumber--)
{
    if (BlankNumber > 0)
    {
        cout << setfill(' ') << setw(BlankNumber) << " ";
    }
    cout << setfill('*') << setw(width) << "*";
    cout << endl;
}
Victoriia
  • 105
  • 9