| |
Main Menu | Next Chapter |
A. Patterns and Purpose Statements
Back at the beginning of this chapter we talked about the need
to design an algorithm before proceeding to code and about the
challenge of writing an algorithm that can easily be translated
into computer code. We then introduced patterns as one aid in
writing such translatable algorithms. We close this chapter with
a discussion of just how to take advantage of these patterns.
As part of the analysis phase for a problem, you discover the
purpose of a program. Notice that the various patterns also have
a purpose. The average pattern, for example, has as its purpose:
Big deal, hu? But, if you compare the purpose of your problem
with the purpose of the various patterns you might find a match!
If you decide, for example, that the problem you have in front
of you needs an average calculated, it is clearly time to pull
out the old average pattern.
Please take advantage of these patterns. If you don't, you will
be 're-inventing the wheel' every time you start a new program.
To summarize then - the steps you know for successfully completing
a programming problem are:
Each of these steps depends on the ones above it and, if you discover
a problem, you may need to go back to an earlier step to fix that
problem. When you think you are finished, make sure that the output
you have matches your specifications. It is not uncommon for programmers
to write a set of specifications and then drift away from them
as they go through the rest of the steps. It is a bit like that
game where one person whispers something into someone's ear and
the 'secret' gets passed from person to person, and finally passed
back to the original person. Usually, the final version of the
secret is quite different from the original.
And, be careful with your testing. For example, in the average problem we just looked at, a good set of tests would have included two or three sets of numbers whose averages were easily calculated. Three test possibilities might be:
In general if your program has multiple
'paths' (different sets of code that are chosen by 'if' and 'while'
test conditions) each of those paths should be tested with a data
set.
B. Wrap Up
In this chapter you have been introduced to algorithm patterns
- an important tool in program development. We have assumed that
our programming problems are simple enough to be solved with one
pattern or some combination of patterns. Real world problems are
usually much more complex and require some kind of 'divide and
conquer' strategy. In the next chapter we
will take a look at one variation of this strategy. However, remember,
as you read the next chapter, that, once a problem is
broken up into parts, solving each of those parts may benefit
from the use of the patterns you learned here.
| |
Main Menu | Next Chapter |