cs1ch3sec7.htm
 

CHAPTER THREE

Analysis and Design : First Steps

 


Section VII: The Average Pattern Section I : Problem Analysis Section II: Problem Analysis Section III: Patterns Section IV:The While Loop With Priming Read Pattern
Section V: The Counter Pattern Section VI: The Sum Pattern Section VIII: The Salary Problem Revisited Section IX: Taking Advantage of Patterns

  


Table of Contents

Learning C++:
An Index of Entry Points


2. The

of C++

A reference document on the basic elements of C++.



3. The Patterns



Index!



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

    Purpose: To calculate the average of some set of numbers.
    Pattern: Use the counter and sum patterns with some loop pattern to determine the count and sum After the loop pattern: if Counter Variable != 0 then { average = Sum Variable / Counter Variable Display average } else { Display some message indicating there was nothing to take the average of. }

The 'if' statement after the while loop is the key addition to this pattern. Inside that 'if' is the division expression that determines the average. Humans can't divide by 0 (How many times can you take zero things from 5 things? Try it!) and neither can computers. In fact, if you ask a computer to divide by zero, you get back an ugly error message - the type you would never want the users of your programs to see. The 'if' statement makes sure that the division is performed only if the divisor is NOT zero.

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."
    }
    

You can see that there is both a counter and sum variable here. To make the code simpler we dropped the second sum variable from our earlier example. We did keep the code that outputs the counter and sum values. Technically, if the manager only wants to know the average, these two outputs would not be required. If this were an actual problem, decisions about exactly what was to be output would be made in the analysis phase. Smart analysts always show their analysis to whoever requested the program before proceeding to the design stage.

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"; } }


Code Examples Involving the Basic Patterns
Code Examples Involving the Logical Operators

Top of Section Main Menu Next Section