A computer program expresses a solution (not all solutions, just one) to a given problem. You must solve the problem before you can write the program! Problem-solving is a learned skill. You learn and develop problem-solving skills through experience. Furthermore, you learn to draw pictures and use them to help solve problems through extensive practice. The picture bridges the problem and a program that solves it. The pyramid example demonstrates how programmers use pictures to help solve a problem.
The Problem
The Solution
Computer scientists use many tools to help solve problems. The following solution uses two of the most common tools: pictures and patterns. Drawing the entire pyramid is unnecessary; the first few rows should be enough to generalize the solution successfully. A picture representing a problem helps a computer scientist solve the problem in at least four different ways:
provides a visualization of the problem that forms a bridge between the problem and an eventual program; a given program is just one of many possible expressions of the problem solution
identifies and organizes the information that the computer scientist has
makes it easier to see and generalize patterns appearing in the problem (i.e., the relationships between the known bits of information)
highlights any missing information
HEIGHT
A symbolic constant for the total pyramid height; change the value of the constant to change the pyramid's height easily. We could also make the height a variable, and, following well-established naming conventions, we would change the spelling to height
level
The pyramid level (or line or row) the program is filling with X's (first dimension)
spaces
The problem's solution requires the program to move the cursor from the left edge of the console to the location of the first X. The program moves the cursor by printing spaces or blanks (made visible in the picture with boxes). The variable spaces is the number of spaces needed to move the cursor (second dimension)
xes
the number of X's needed to fill the current level or row of the pyramid (third dimension)
Picture
Relationships
level
spaces
xes
0
3
1
1
2
3
2
1
5
3
0
7
General
HEIGHT - level - 1
2 * level + 1
Problem solving with pictures.
It isn't necessary to include the entire problem in the picture. We can identify the relationships between HEIGHT, level, spaces, and xes from the top four levels.
The variable level is the independent variable: it doesn't depend on other variables. As the level increases, the number of spaces on each row of the pyramid decreases by 1, and the number of Xs increases by 2. This observation implies that spaces and xes are each a function of level (they are dependent variables): spaces = f(level) and xes = g(level).
Subtraction and division cause the dependent variable to decrease as the independent variable increases - a constant decrease by 1 suggests subtraction. Alternatively, addition and multiplication cause the dependent variable to increase with the independent variable - increasing at a constant rate of 2 suggests multiplication by 2. However, there is a problem: xes is always odd and 2 × odd = even and 2 × even = even. We solve this problem by adding 1. With some trial and error, we discover:
spaces = f(level) = HEIGHT - level - 1
xes = g(level) = 2 * level + 1
The key to translating the solution above into the code below is recognizing that the for-loops are just counting characters. The formulas in the table and the picture represent how many characters the program should print - how many times each loop should iterate or repeat.