Quiz 3 Solutions

  1. Why do we use pseudo-code to document an algorithm, rather than a natural language like English or a programming language like Python?

    Pseudo-code is easier for humans to understand than a programming language, and more precise than a natural language. It was a common misconception that computers can understand pseudo-code: they cannot.

  2. 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.

    1. Let N be 8
    2. Set I to 1
    3. If I > N then stop.
    4. If I < 4 or I is odd, then output I
    5. Set I to I+1
    6. Go back to step 3.

    The output would be: 1 2 3 5 7

  3. Give an example of each kind of pseudo-code instruction. Refer to the relevant step numbers in the algorithm above.

    1. Input: Usually an instruction where we say “let,” like step 1 above except that it doesn’t allow the user to decide what N should be.

    2. Output: Any statement that says “output”, like part of step 4.

    3. Variable assignment: Statements that say “set”, like 2 and 5.

    4. Conditional: Statements that say “if/then/else/otherwise”, like steps 3 and 4.

    5. Loop: Any statement that repeats (goes back to) a previous step, like step 6.

 

©2012 Christopher League · some rights reserved · CC by-sa