Problem 8 is very similar to problem 7, so the hints for the current problem are similar to the problem 7 hints. However, there is one significant difference between the two problems: in problem 7 there are a series of spaces at the end of each line, but the program doesn't need to print them.
However, in problem 7, the spaces are at the beginning of each line, and our program must print those spaces. Doing this requires one additional nested for-loop, totaling three loops - one outer loop with two nested inside. The added is the first of the nested loops. The number of spaces must decrease as the outer loop moves from the top to the bottom. So, the space loop will count backward. A picture often helps us discover what steps are needed to solve a problem and helps us to organize those steps in the final program.
Programs displaying characters on the console must always print them from left to right and top to bottom. They print the space character to move the cursor to the right without printing a visible character. The illustrations below make the space characters visible.
|
||||||||||||||||||||||||||||||||||||||||||||||||||
The bullets represent spaces, making them visible |
|
for (top row to bottom row) { for (left edge of window to left side of pyramid) cout << space for (left side of the pyramid to its right side) cout << number (outer loop control variable) }