Star Pyramid Manipualtion
Jun 7, 2014 at 5:52pm UTC
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;
}
Jun 7, 2014 at 5:59pm UTC
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.
Jun 7, 2014 at 6:06pm UTC
What was the row*2+1 code for?
Jun 7, 2014 at 6:08pm UTC
It was for program with slightly different behavior as your code in OP post.
Jun 7, 2014 at 6:10pm UTC
Thanks a million for the help! Appreciate it! :)
Last edited on Jun 7, 2014 at 6:58pm UTC
Jun 7, 2014 at 8:23pm UTC
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?
Jun 7, 2014 at 8:26pm UTC
Easiest way: after you read row, do row = row* 2 - 1;
Jun 7, 2014 at 8:49pm UTC
Thanks again!
Topic archived. No new replies allowed.