Counting
Algorithm
Development
Counting mechanisms are very frequently used in computer
algorithms. Generally a count must be made of the number of items in a set
which possess some particular property or which satisfy some particular
condition or conditions. This class of problem is typified by the “examination
marks” problem.
As a starting point for developing a computer algorithm for
this problem we can consider how we might solve a particular example by hand.
Suppose thar we are given the set of marks.
55, 42, 77, 63, 29, 57, 89
To make a count of the passes for this set we can start at the
left, examine the first mark (i.e. 55), see if it is ≥50, and
remember that one student has passed so far. The second mark is then examined
and no adjustment is made to the count. When we arrive at the third mark we see it is ≥50 and so
we add one to our previous count. The process continues in a similiar manner
until all marks has been tested.
In more detail we have:
Algorithm
Description
1.
Prompt and then read the number of marks to be
processed
2.
Initiaize count to zero
3.
While there are still marks to be processed
repeatedly do
(a)Read next marks,
(b)
If it a pass (i.e. ≥50) then add one to count
4.
Write out total of passes
Pascal
Implementation
Notes on
design
1.
Initially, and each time through the loop, the
variable count represents the number of passes so far en countered.
2.
It was possible to use subtitution to improve on the
original solution to the problem.
3.
An end-of-line test is included to handle multiple
lines of data.
Applications
All forms of counting.
No comments:
Post a Comment