SAC EXAMPLE 7
Example of the  Nesting of Iteration Constructs

The following SAC represents the algorithm of nested for loops.  (a) Do a trace of the algorithm for an input of 4.  What does the output look like? (b) What does the algorithm do?  (c) Of what do you think the for loop indices, R and C, are abbreviations?


 
 
 

EXTENSIONS:

  1. Implement the code in the language you are studying.
  2. Modify the SAC so that it would, for an input of 4, efficiently produce the output the output:
    1. 1
      22
      333
      4444
  3. Modify the SAC so that the output would appear upside down from the output in Extension 2.
  4. Modify the SAC so that a while loop is used to accomplish the same task as the outer for loop.  (HINT: see extension 7 in SAC EXAMPLE 6.)
  5. Modify the SAC to accept three inputs L$ (for a character), W (for an integer), and H (for an integer) and hence output a box of the input letters with a width W and a height H.
  6. In general, what would the SAC look like for any two dimensional illustration "drawn" by printing a single character at a time, like the preceding examples?  Can you draw a single generic SAC for drawing any such illustration?
  7. 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: In general, what does an algorithm of (a)  two nested loops and (b) three nested loops represent?
TPQ: What do you think the next extension would be?  (Hint: what do you think Tony will ask on an assessment of this this example?)