|
Table of Contents
Learning C++:
An Index of Entry Points
2. The A reference document on the basic elements of C++.
3. The Patterns
|
Section VI. The Sum Pattern
Pattern: Initialize the Sum variable to 0
Use some loop pattern - see Sections 'B' and 'C' above
Inside the loop pattern add the following code:
Sum variable = Sum Variable + Amount To Add Using this pattern we can create an algorithm
to add up all the discounted prices
You may have already figured out just by reading this that the Total
Discounted Price is calculated by determining each individual discounted
price each time through the loop and then adding (each time through the
loop) that amount to whatever value is already in Total Discounted Price.
That is why this variable needs to be initially set to zero. You should
also note that there really are two patterns here - the "while loop with a
priming read" pattern and the sum pattern. Finally, note that the output
of the total is after the while loop. Were the output to be placed inside
the loop, the user would see the total continuously, as it was being
updated. If any of this is unclear, you are strongly urged to trace the
algorithm. Try moving around or deleting various lines in the algorithm
and see what happens when you trace. Let's make the problem a bit harder before we translate the algorithm
into C++ code. Suppose the manager of the store wants to know the total
amount that was discounted - the difference between the total original
price and the total price after the discount. One way to accomplish this,
as hinted by the very description just given (an advantage in writing a
clear problem description as part of the analysis phase) would be to add
up all the original prices, add up all the discounted prices, and
calculate the difference. Here is the modified algorithm:
As you can see, we now have two total variables. An algorithm can have
as many totals as it needs. Also, each total/sum calculation can occur
anywhere inside the while loop as long as the value to be summed has
already been calculated or somehow entered into the appropriate variable.
In this case, we could have moved the line:
to the very top of the 'while' loop - just after the "while Price !=
-1" line. Of course, it could not be the last line in the while loop
because we would then have lost the first price and at the end we would be
adding -1 to the total. (If this is not clear, you know what to do.) The other summation line could be placed anywhere inside the loop after
the line that calculates the Discounted Price. Note that 'Total Discount'
must be calculated after the 'while' loop but it could be placed anywhere
from the end of the loop to just before its output line. What you are
discovering here is that a particular line of code does not have only one
spot in which it can be placed. On the other hand, there is usually some
limit to where it can be placed. There is more than one way to write a
program but there are definitely 'wrong' ways. The translation of this algorithm into C++ is straight forward. As a
reminder, however, remember that "X = X + Y" can be translated as "X += Y".
int discountedPriceTotal = 1234;
|
|