Problem 8: Pyramid Of Numbers 2 Hints

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.

Visualizing The Pyramid 2 Problem

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.

1
22
333
4444
55555
666666
7777777
A 7 by 7 grid. Each cell in the grid contains a space or a digit. Row one has 7 spaces from left to right and ends with the digit '1.' Row two has six spaces and two digits, both 2's. The pattern continues to row seven, with no spaces and seven digits, all 7's.
The bullets represent spaces, making them visible
  • Each small square represents a single character as it is displayed on the console window
  • Empty squares represent the space character

Pyramid 2 Pseudo-Code

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)
}