SAC EXAMPLE
6
Example of a Count
Controlled Definite Loop.
(
NOTE:
This does not implement the OOSD paradigm; see the last extension.)
The (imperfect) SAC below...
EXTENSIONS:
-
Implement this algorithm in the language of your course.
-
The factorial of 0 is defined to be 1. Does
this algorithm above work for N=0? If not, modify the algorithm so
that is gives the correct answer.
-
The factorial of a negative number is undefined.
Add a error trap to the above SAC that, if a negative value of N
is input, the algorithm will output an error message and halt.
-
Rather than halt the program if a negative number is input
(as in extension 3), add an error trap that would keep prompting the
user to input a positive number.
-
Add another for loop to the algorithm so that the factorial
of the first 10 positive integers would be output as a list (Hint:
you might need to preview SAC EXAMPLE 7
before doing this.)
-
The factorial of fractional numbers is undefined. Add
an error trap that prevents anything other than positive integers being
processed. You may assume that the implementation language has
the facilities to perform any mathematical operations you need.
-
Modify the SAC so that a while loop is used
to accomplish the same task. (HINT: three statements will be needed
to replace the for construct.
-

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
1: In general, what does an algorithm of (a) two nested loops and
(b) three nested loops represent?