Previous Section Main Menu Next Section

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

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

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.

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:


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

Top of Section Main Menu Next Section