Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- int height,help;
- height = 6; //the height of the pyramid
- help = 0; //assistance variable in the for loops later
- for (int i = 1; i <= height; i++) //i represents the row of the pyramid
- {
- for (int j = 1; j <= (height *2)-1; j++) //j represents the column of the pyramid
- {
- if (j == (height)) //in this case, height is also the index of
- //the middle of the pyramid
- cout << "*";
- else
- if (j>=(height - help) && j<=(height + help))
- /*
- Alright so after every row, number of stars in the columns increases by 2
- One on the left and one on the right. Helper variable starts at 0, and increases
- by 1 ever loop. In this case, height - help and height + help help us determine
- the index of the columns at which we start writing out *s, and index at which we stop at.
- After every loop, the start and end of the columns increase by 1, meaning 1 extra
- star at the start and the finish of the row.
- */
- cout << "*";
- else
- cout << " "; //This means if none of the above if clauses are true,
- //the program will write out a blank space by default.
- }
- cout << endl; //This cout is inside the for loop with the i, but outside the
- //j for loop, which means every time the j loop ends, the cursor goes to the next line
- help++;
- }
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement