SAC EXAMPLE
5
Example of an Indefinite
Loop, Accumulation, and Counting
(
NOTE:
This does not implement the OOSD paradigm; see the last extension.)
The SAC below represents a classic numeric processing
task implemented using a while loop, a special case of indefinite iteration.
(a) Fill in the declaration table in the SAC. (b) Perform a trace
of the algorithm for the data 10, 20, 30, and 40; assume an "end of data"
mark (EOD in the while construct) follows the last value. (c) Identify
instructions in which (1) counting and (2) accumulation (in this case,
summing) takes place. (d) What does this algorithm do? (Fill
in the start symbol.) (e) The two instructions immediately before
the while loop initialize the variables C and T; what would happen if these
instructions were omitted?
EXTENSIONS:
-
Implement the algorithm with the language of your
course.
-
Modify the algorithm, replacing the while loop with
a repeat-until loop.
-
Rewrite the algorithm using a definite loop.
(HINT: see SAC EXAMPLE 6; the number of values to be processed must be
input before the loop.
SAQ:
Do you see the distinction between the indefinite loop and definite loop?)
-
The method used to determine EOD (End Of Data) is
unspecified. The ease of doing this is highly language dependent.
However, all languages can handle the case of processing only positive
numbers, i.e. by terminating the loop with a negative number. Draw
the SAC for such an algorithm.
-
Modify the algorithm to incorporate the facility of determining
the highest and lowest values in the input list.
-
Modify the algorithm by incorporating an "error trap"
that will prevent numbers outside the range 0 <= N <= 100
from being processed.
-

How
would the algorithms in this example be modified to represent OOSD?
(b) Would the different versions of this algorithm have different OOSD
versions?
SAQ