Star Pyramid Manipualtion

Pages: 12
If I have the following code that produces a center aligned pyramid, how can i remove the middle stars of the pyramid so that I have just the outline?
See picture at end of code for example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include<iostream>
using namespace std;

int main(){

    int x,y;
    char star = '*';
    int spaces = 0;
    int row;

    cout << "Enter number of rows" << endl;
    cin >> row;

    for(x=1; x <= row; x++)
    {
        if((x%2) != 0)
            {
                spaces = (row - x) / 2;  // Calculate number of spaces to add

                for(int i = 0; i < spaces; i++) // Apply spaces
                {
                    cout << " ";
                }

                    for(y=1; y <= x ; y++)
                    {
                        cout << star;
                    }

                        cout << endl;
            }
    }
    return 0;
}

row=5:
    *
   * *
  *   *
 *     *
*********
output single star, output x - 2 spaces, output another star.
I'm not understanding what you mean..
1
2
3
4
                    for(y=1; y <= x ; y++)
                    {
                        cout << star;
                    }

Here you are outputting x stars
Instead output one star (it will be left one), then outpust spaces instead of middle stars, then output another star (it will be the right one)

You will need to handle first and last rows differently.
So I should output a single star outside the loop. The spaces in the loop and the second star outside the loop?
Something like this?

1
2
3
4
5
6
7
8
cout << star;

     for(y=1; y <= x-2; y++)
     {
        cout << " " ;
      }

     cout << star;
Last edited on
yes. But as I said you should handle first (only one star) and last (no spaces, stars instead) rows separately.
In two seperate loops?
Nope, just like you done with stars/spaces:
first row handling
loop for middle loop handling
last row handling
Is my loop correct?
Yes, it will output correct values for rows in the middle of the triangle. Why don't you just test it?

Bonus: Program written with stream manipulators: http://ideone.com/IS2b9o
I did test it. Was just making sure. Except it outputs like:
1
2
3
4
 
  **
 * *
*   *


I'm not sure about filling in the last row. Tried but nothing worked.
Also for the first row am i supposed to put a space then the star?
You should handle first and last row separately.
so you should output row/2 spaces, then star (first row);
Then run your loop with x starting from 2;
Then output row*2 + 1 stars (last row)
Could you please show me? This is not working out for me. And how are you outputting the last row. Because when I use that piece of code it changes the stars to number instead
11
12
13
14
15
16
17
18
19
    for(int i = 0; i < row / 2; ++i)
        std::cout << ' ';
    std::cout << star << '\n';
    for(x=2; x <= row; x++)
    {
        //rest of loop
    }
    for(int i = 0; i < row*2 + 1; ++i)
        std::cout << star;
Last edited on
Where it says "rest of loop" Would you insert another loop?
No. There should be loop which you already made and it is working for you. That loop which in your original code was on lines 14-32 and which you modified later .
If you saying I should put that loop inside here it does not work unless I'm mis-understanding
How did you place it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
using namespace std;

int main(){

    int x,y;
    char star = '*';
    int spaces = 0;
    int row;

    cout << "Enter number of rows" << endl;
    cin >> row;

    for(x=1; x <= row; x++)
    {
                for(int i = 0; i < row / 2; ++i)
                cout << ' ';

                cout << star << '\n';

                    for(x=2; x <= row; x++)
                    {
                        if((x%2) != 0)
                        {
                            spaces = (row - x) / 2;  // Calculate number of spaces to add

                            for(int i = 0; i < spaces; i++) // Apply spaces
                            {
                                cout << " ";
                            }

                                for(y=1; y <= x-2; y++)
                                {
                                    cout << " " ;
                                }

                                cout << endl;
                         }
                     }

                            for(int i = 0; i < row*2 + 1; ++i)
                            cout << star;

                        cout << endl;
                }

    return 0;
}
I asked you to place loop inside provided code, not provided code inside loop.
Remove lines 14, 15 and 45;
and I do not see star output inside your loop like you show in this message: http://www.cplusplus.com/forum/beginner/135010/#msg720770
Pages: 12