Problem Solving
Problem Solving
You keep going until the balance is at least $20,000. Then the last number in the year
column is the answer.
Of course, carrying out this computation is intensely boring to you or your
younger brother. But computers are very good at carrying out repetitive calcula-
tions quickly and flawlessly. What is important to the computer is a description of the
steps for finding the solution. Each step must be clear and unambiguous, requiring no
guesswork. Here is such a description:
Start with a year value of 0, a column for the interest, and a balance of $10,000.
Repeat the following steps while the balance is less than $20,000
Add 1 to the year value.
Compute the interest as balance x 0.05 (i.e., 5 percent interest)
Add the interest to the balance.
14 942.82 19799.32
15 989.96 20789.28
Pseudocode is an Of course, these steps are not yet in a language that a computer can understand, but
informal description you will soon learn how to formulate them in C++. This informal description is called
of a sequence of pseudocode.
steps for solving a
problem. There are no strict requirements for pseudocode because it is read by human read-
ers, not a computer program. Here are the kinds of pseudocode statements that we
will use in this book:
• Use statements such as the following to describe how a value is set or changed:
total cost = purchase price + operating cost
or
Multiply the balance value by 1.05.
or
Remove the first and last character from the word.
• You can describe decisions and repetitions as follows:
If total cost 1 < total cost 2
While the balance is less than $20,000
For each picture in the sequence
Use indentation to indicate which statements should be selected or repeated:
For each car
operating cost = 10 x annual fuel cost
total cost = purchase price + operating cost
Here, the indentation indicates that both statements should be executed for
each car.
• Indicate results with statements such as:
Choose car1.
Report the final year value as the answer.
The exact wording is not important. What is important is that pseudocode describes a
sequence of steps that is
• Unambiguous
• Executable
• Terminating
Figure 9
Understand The Software Development Process
the problem
Develop and
describe an
algorithm
Test the
algorithm with
simple inputs A method is unambiguous when there are precise
instructions for what to do at each step and where
to go next. There is no room for guesswork or cre-
Translate ativity. A method is executable when each step can
the algorithm
be carried out in practice. Had we asked to use the
into C++
actual interest rate that will be charged in years to
come, and not a fixed rate of 5 percent per year, our
method would not have been executable, because
Compile and test there is no way for anyone to know what that
your program interest rate will be. A method is terminating if it
will eventually come to an end. In our example, it
requires a bit of thought to see that the method will
not go on forever: With every step, the balance An algorithm is a recipe for
goes up by at least $500, so eventually it must reach finding a solution.
$20,000.
An algorithm for A sequence of steps that is unambiguous, executable, and terminating is called an
solving a problem is algorithm. We have found an algorithm to solve our investment problem, and thus
a sequence of steps we can find the solution by programming a computer. The existence of an algorithm
that is unambiguous,
executable, and is an essential prerequisite for programming a task. You need to first discover and
terminating. describe an algorithm for the task that you want to solve before you start program-
ming (see Figure 9).
SELF CHECK 20. Suppose the interest rate was 20 percent. How long would it take for the invest-
ment to double?
21. Suppose your cell phone carrier charges you $29.95 for up to 300 minutes of
calls, and $0.45 for each additional minute, plus 12.5 percent taxes and fees. Give
an algorithm to compute the monthly charge from a given number of minutes.
22. Consider the following pseudocode for finding the most attractive photo from a
sequence of photos:
Pick the first photo and call it "the best so far".
For each photo in the sequence
If it is more attractive than the "best so far"
Discard "the best so far".
Call this photo "the best so far".
The photo called "the best so far" is the most attractive photo in the sequence.
Is this an algorithm that will find the most attractive photo?
23. Suppose each photo in Self Check 22 had a price tag. Give an algorithm for find-
ing the most expensive photo.
24. Suppose you have a random sequence of black and white marbles and want to
rearrange it so that the black and white marbles are grouped together. Consider
this algorithm:
Repeat until sorted
Locate the first black marble that is preceded by a white marble, and switch them.
What does the algorithm do with the sequence mlmll? Spell out the steps
until the algorithm stops.
25. Suppose you have a random sequence of colored marbles. Consider this
pseudocode:
Repeat until sorted
Locate the first marble that is preceded by a marble of a different color, and switch them.
Why is this not an algorithm?
Practice It Now you can try these exercises at the end of the chapter: R1.13, R1.14.
Before you are ready to write a program in C++, you need to develop an algorithm—a method
for arriving at a solution for a particular problem. Describe the algorithm in pseudocode: a
sequence of precise steps formulated in English.
For example, consider this problem: You have the choice of
buying two cars. One is more fuel efficient than the other, but also
more expensive. You know the price and fuel efficiency (in miles
per gallon, mpg) of both cars. You plan to keep the car for ten
years. Assume a price of $4 per gallon of gas and usage of 15,000
miles per year. You will pay cash for the car and not worry about
financing costs. Which car is the better deal?
CHAPTER SUMMARY
• The central processing unit (CPU) performs program control and data
processing.
• Storage devices include memory and secondary storage.