Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int height,help;
  6. height = 6; //the height of the pyramid
  7. help = 0; //assistance variable in the for loops later
  8. for (int i = 1; i <= height; i++) //i represents the row of the pyramid
  9. {
  10. for (int j = 1; j <= (height *2)-1; j++) //j represents the column of the pyramid
  11. {
  12. if (j == (height)) //in this case, height is also the index of
  13. //the middle of the pyramid
  14. cout << "*";
  15. else
  16. if (j>=(height - help) && j<=(height + help))
  17. /*
  18. Alright so after every row, number of stars in the columns increases by 2
  19. One on the left and one on the right. Helper variable starts at 0, and increases
  20. by 1 ever loop. In this case, height - help and height + help help us determine
  21. the index of the columns at which we start writing out *s, and index at which we stop at.
  22. After every loop, the start and end of the columns increase by 1, meaning 1 extra
  23. star at the start and the finish of the row.
  24. */
  25. cout << "*";
  26. else
  27. cout << " "; //This means if none of the above if clauses are true,
  28. //the program will write out a blank space by default.
  29. }
  30. cout << endl; //This cout is inside the for loop with the i, but outside the
  31. //j for loop, which means every time the j loop ends, the cursor goes to the next line
  32. help++;
  33. }
  34. cout << endl;
  35. system("pause");
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement