Quiz 3 solution

  1. Carefully trace the following algorithm. What does it output? Be sure to mark a section of your answer as the output, separate from your scratch work.

    step 1. Let N be 8
    step 2. Set I to 1
    step 3. If I > N then stop.
    step 4. If I < 4 or I is odd, then output I
    step 5. Set I to I+1
    step 6. Go back to step 3.
    • N: 8
    • I: 1 2 3 4 5 6 7 8 9
    • Output:
    1
    2
    3
    5
    7
  2. For each type of statement below, indicate which step(s) from the algorithm in question 1 contain that type of statement.

    1. Output: step 4 contains output.
    2. Variable assignment: steps 1, 2, 5 involve variable assignment.
    3. Conditional: steps 3 and 4 are conditionals.
    4. Loop: step 6 creates a loop.

     

  3. Below is an illustration of an array and two other variables. What are the values of each of the following expressions?

    1. A[0] = 3
    2. A[X] = A[2] = 4
    3. A[Y]+1 = A[5]+1 = 7+1 = 8
    4. A[X+1] = A[2+1] = A[3] = 0
    5. A[A[2]] = A[4] = 2