Star Pyramid Manipualtion

Pages: 12
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
49
#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(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 << " ";
                            }   
                            
                                cout << star;

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

                                cout << endl;
                         }
                     }

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

                        cout << endl;
                        
    return 0;
}
line 19 should be for(x=2; x < row; x++)
line 43 change to for(int i = 0; i < row ; ++i)
And it works like your original code but with spaces inside.
What was the row*2+1 code for?
It was for program with slightly different behavior as your code in OP post.
Thanks a million for the help! Appreciate it! :)
Last edited on
I have one other question. The code works by making the last row equal to n stars with less than n rows. How do I alter the code to make n rows instead?
Easiest way: after you read row, do row = row* 2 - 1;
Thanks again!
Topic archived. No new replies allowed.
Pages: 12