Assignment 3

Due Monday 15 October in class.

Part one

Read the pseudo-code below and answer the questions that follow.

    step 1. let Y be X + 5
    step 2. if Y > 0 then set X to Y * 2
    step 3. set X to X * 3
    step 4. output X
  1. Does this algorithm contain a conditional statement? If so, which one?
  2. Does this algorithm contain iteration? If so, which steps repeat?
  3. Trace the algorithm with the input X = 4. What does it output?
  4. Trace the algorithm with the input X = -3. What does it output?
  5. Trace the algorithm with the input X = -7. What does it output?

Part two

Here, C refers to a sequence of variables (an array), and the notation C[I] uses the value of I to determine which C to access.

    step 1. set K to C[1]
    step 2. set N to 1
    step 3. set I to 2
    step 4. if I > 7 then output N then K and stop
    step 5. if C[I] = K then set N to N+1 and go to step 9
    step 6. output N then K
    step 7. set K to C[I]
    step 8. set N to 1
    step 9. set I to I+1
    step 10. go back to step 4.

Below are the initial values of the array, and other variables you will use.

      K :
      N :
      I :
   C[1] : apple
   C[2] : apple
   C[3] : banana
   C[4] : carrot
   C[5] : carrot
   C[6] : carrot
   C[7] : date