Star Pyramid Manipualtion

Pages: 12
Jun 7, 2014 at 1:57pm
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:
    *
   * *
  *   *
 *     *
*********
Jun 7, 2014 at 2:21pm
output single star, output x - 2 spaces, output another star.
Jun 7, 2014 at 3:14pm
I'm not understanding what you mean..
Jun 7, 2014 at 3:25pm
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.
Jun 7, 2014 at 3:41pm
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 Jun 7, 2014 at 3:45pm
Jun 7, 2014 at 3:47pm
yes. But as I said you should handle first (only one star) and last (no spaces, stars instead) rows separately.
Jun 7, 2014 at 3:59pm
In two seperate loops?
Jun 7, 2014 at 4:01pm
Nope, just like you done with stars/spaces:
first row handling
loop for middle loop handling
last row handling
Jun 7, 2014 at 4:05pm
Is my loop correct?
Jun 7, 2014 at 4:11pm
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
Jun 7, 2014 at 4:20pm
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?
Jun 7, 2014 at 4:26pm
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)
Jun 7, 2014 at 4:42pm
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
Jun 7, 2014 at 4:45pm
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 Jun 7, 2014 at 4:46pm
Jun 7, 2014 at 5:00pm
Where it says "rest of loop" Would you insert another loop?
Jun 7, 2014 at 5:02pm
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 .
Jun 7, 2014 at 5:25pm
If you saying I should put that loop inside here it does not work unless I'm mis-understanding
Jun 7, 2014 at 5:27pm
How did you place it?
Jun 7, 2014 at 5:37pm
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;
}
Jun 7, 2014 at 5:43pm
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