| |
Main Menu | Next Section |
Section VII. One More Pattern: The Average Pattern
As part of the example of the sum pattern we took out any references
to the counter pattern. It is actually quite common for both a
counter and a sum to be required in the same problem and therefore
for both patterns to be required in the same problem. A perfect
example of this is any problem requiring an average. As you know,
an average is calculated by dividing the sum of some set of numbers
by the number of items summed - the total divided by the count.
The very act of analyzing the requirements of an average problem
tells us that we need the counter and sum patterns and from that
we know we need a loop pattern because both the sum and counter
patterns talk about the need for a loop pattern.
You might be tempted to conclude that since a loop pattern is
mentioned in both the counter and sum patterns, the algorithm
for an average problem will require two loops. This is where
you need to be careful when combining patterns. If you think about
it carefully, you realize that the loop pattern's purpose in both
the counter and sum patterns is the same - to handle, one at a time, the
items being counted or summed. In other words, the same loop meets
both goals - it brings in the items to be summed and allows them
to be counted. Therefore, we only need one loop pattern.
The average pattern uses all three of the patterns already discussed
and adds the average calculation itself at the end:
Pattern Name: The Average Pattern
As the pattern is written, no average is taken if there are no
items to average. In this case a message is output. There are
other ways of handling this situation. For example, you could
set the average to 0.
To make this discussion concrete, suppose that the manager of
the store we have been working with wants to know the average
discounted Price. To calculate this we need the Total Discounted
Price and the number of discounted items - two values we have
already worked with. Here is the algorithm for this pattern. Note
that most of it is simply a combination of the two algorithms
we have just seen.
Set Item Count to 0
Set Total Discounted Price to 0
Get Price
while Price != - 1
{ Discounted Price = Price * Discount
Output Discounted Price
Total Discounted Price = Total Discounted Price + Discounted Price
Item Count = Item Count + 1;
Get Price
}
Output Total Discounted Price
Output Item Count
If Item Count != 0 then
{
discountedPriceAverage = Total Discounted Price / Item Count;
output discountedPriceAverage;
}
else
{
output "There were no discounted items to average."
}
Assuming that we have completed our analysis and design, let's
proceed to the code for this problem:
// A program to calculate sale prices based on a single discount percentage and output the
// number of items discounted, the total of the discounted prices, and
// the average of the discounted prices.
// File: ch3prg4.cpp
// Use the Specifications from the previouis version and add a
// description of the average.
#include <iostream.h>
const double DISCOUNT = .87;
void main()
// Purpose: to calculate sale prices based on a single discount percentage
// and output the number of items discounted, the total of the discounted
// prices, and the average of the discounted prices.
// Receives: NONE
// Returns: NONE
{
double price;
double discountedPrice;
double averageDiscountedPrice;
double discountedPriceTotal = 0;
double originalPriceTotal = 0;
int itemCount = 0;
cout << "Please enter an item's original price (Enter -1 to Stop) ";
cin >> price;
while (price != -1)
{
discountedPrice = price * DISCOUNT;
cout << "The New Price is: $" << discountedPrice << endl;
discountedPriceTotal += discountedPrice;
itemCount++;
cout << "Please enter an item's original price (Enter -1 to Stop) ";
cin >> price;
}
cout << "The total of the discounted prices is: $" << discountedPriceTotal << endl;
cout << "The number of items discounted is: " << itemCount << endl;
if (itemCount != 0)
{ averageDiscountedPrice = discountedPriceTotal / itemCount;
cout << "The average discountedPrice is: $" << averageDiscountedPrice
<< endl;
}
else
{ cout << "There were no discounted items to average.\n";
}
}
| |
Main Menu | Next Section |