Unit 2: Programming Design

Lesson 8: Pseudocode

Notebook Authors

(fill in your two names here)

Facilitator: (fill in name)
Spokesperson: (fill in name)
Process Analyst: (fill in name)
Quality Control: (fill in name)

If there are only three people in your group, have one person serve as both spokesperson and process analyst for the rest of this activity.

At the end of this Lesson, you will be asked to record how long each Model required for your team. The Facilitator should keep track of time for your team.

Computational Focus: Designing Pseudocode

Recall the basic steps of the program design process:

  1. Understand the problem.
  2. Design an algorithm to solve the problem. If the problem is large, break the problem into steps.
  3. Write code to solve each step of the problem.
  4. Test your code.
  5. Debug your code.

In step two, an algorithm, or step by step procedure, is developed to solve the desired problem. As you design an algorithm, you will need to identify the kinds of programming construct that will be used to form the basic structure of your code. Based on what has been introduced so far, this may include:

  • sequential code - every line runs exactly once (no construct at all)
  • branching code - one or more line might run once or not at all (if statement)
  • predetermined number of loops - one or more lines will run repeatedly for a predetermined number of times (for loop)
  • unknown number of loops - one or more lines will run one or more times, based on a boolean condition (while loop)

Model 1: Pseudocode

Pseudocode is a text explanation of your algorithm that is more easily translated on Python code. Here is an example of pseudocode:

initialize all variables
prompt the user for a value 
loop as long as the total is less than 10
    update the running total (sum)
    increment the number of values entered
    prompt the user for another value
assign mean to total / number of values
return mean

Pseudocode is a useful way to design a solution to a problem without getting caught up in getting all the syntax details correct. Notice it is indented just like Python code.

Critical Thinking Questions

1. What does it mean to initialize a variable (the first line of pseudocode)? Provide one example line of code.

2. Describe the primary purpose of this algorithm (i.e., the goal of the program).

3. What kind of programming construct would be most appropriate handle the loop in the pseudocode? Usually, instead of using the word “loop as long as”, a programmer will write while or for. Rewrite this line of pseudocode using the correct type of loop.

4. Describe the advantage of writing pseudocode that describes the loop execution as “as long as the total is less than 10” rather than “loop until total is greater than 10”?

5. Suppose the problem changes so you want to calculate the sum of a preassigned list of numbers. Identify a possible change to the basic programming construct that could be implemented for this algorithm.

6. Write an example of pseudocode that would implement this new algorithm (to calculate the sum of a list of numbers). (don't forget about the easy formatting tricks for code or look at the markdown in Model1)

7. Now write a statistics function that takes as a list of numbers as its parameter. Using your pseudocode from the previous question as a reference, write the code to calculate and print the mean of your list of number (don't worry about total < or > than 10). Your code should include a loop. Test and debug using the list [5, 10, 13].

8. Copy and paste your pseudocode from question 6 here. Modify it to calculate the variance of a list of numbers. When modifying the psudocode, you should carefully consider which variance equation in the pre-activity would not require an additional loop. Your algorithm should print both the mean and variance.

Model 2: Python code

Run the following Python code:

def model2():
  """this function sums up numbers until they are 10 or higher"""
  total = 0
  value = int(input("Please enter a value: "))
  while total < 10:
    print "value is", value
    total = total + value
    if total < 10:
      value = int(input("Please enter another value: "))
  return total

Critical Thinking Questions

9. Describe the primary purpose of the above model2 function.

10. Identify the primary difference between the basic structure of the model2 code above and the pseudocode given in Model 1.

11. Assume the numbers 5, 2, and 7 are entered.

11a. How many times does "value is" display, when the numbers 5, 2, 7 are entered?

11b. How many times does "Please enter another value: " display?

11c. Do all of the lines of code in the while loop execute the same number of times? Why or why not?

12. If the second input command was not nested inside an if-statement but was just part of the while-loop, what incorrect behavior would you see?

13. Explain why this programming structure might be referred to as a loop and a half.

14. Think about how to identify the maximum value in a list. Your algorithm should work on lists, no matter where the maximum value falls. For example, it should work for the following test cases:

  • [5, 10, 13]
  • [13, 10, 5]
  • [10, 13, 5]

It should also work if all the values are negative, as in:

  • [-5, -10, -13, -3]

14a. What type of programming construct is most appropriate for your algorithm?

14b. What value should the maximum value be initialized to?

15. Develop pseudocode to identify and print the maximum value in the list. (don't forget about the formatting tricks, if you want a refresher, look at the markdown for the pseudocode in Model1) Show your pseudocode to an instructor before moving on.

Model 3: Ordering code inside loops

Nesting an if-statement inside the loop was useful in the previous Model to prevent prompting for an additional (unused) number after the total is greater than 10. In some cases, however, the same effect can be achieved by simply structuring the body of the loop differently. The key to restructing loops is recognizing that the last line of a loop will execute just before the first line of the loop (in the next iteration).

Here again is the pseudocode from Model 1:

initialize all variables
prompt the user for a value
loop as long as the total is less than 10
    update the running total (sum)
    increment the number of values entered
    prompt the user for another value
assign mean to total / number of values
return mean

Critical Thinking Questions

16. Examine the Model 1 pseudocode.

16a. Which Python "commands" appear more than once in the pseudocode (don't think about how it runs, but just look at the words that could be functions/operations/instructions/etc.)?

16b. Explain why the provided pseudocode requires both lines you circled. Why is it insufficient to only include this line once inside the loop?

17. It is possible to change the sequence of commands such that the repeated line is only needed once. Write the pseudocode below that would implement this new algorithm.

18. Explain why this change would be impossible if the looping pseudocode in Model 1 was “loop as long as value is a positive number“.

Model 4: Nested loops

It is often useful to place a loop inside a loop, known as nested loops. The first loop is known as the outer loop, and the second (more indented) loop is known as the inner loop.

def model4(n):
    for i in range(n):
        print("for i =",i)
        for j in range(n):
            print("for j =",j,"and i=",i)
    print("loops done!")

There is no limit to the number of loops that you can nest inside each other.

Critical Thinking Questions

19. Run model4 multiple times (with different parameters).

19a. Does the variable i or j control the number of times the outer loop executes?

19b. Which loop executes more times, the inner loop or the outer loop? Explain your reasoning.

20. In terms of n:

20a. how many times does the outer loop execute?

20b. how many times does the inner loop execute?

21. If you were to change the list of the inner loop from range(n) to range(i), write your team’s prediction below for the expected output for the new series of print statements that will display for n = 3. After writing your prediction, check your answer.

22. Copy and paste the model4 function. Replace the inner loop (for j in) with a while loop. The output for this modified function should be the same as the original model4 function.

23. The mode of a list of numbers is the number that appears the most times in the list. Think about how you would determine the mode from a long list of numbers.

23a. What programming construct is needed?

23b. Copy and paste your statistics pseudocode from Model 1 here. Modify it to also calculate and print the mode of a list of numbers. If more than one number appears the same number of times, print the first number that appears that many times (so the mode for the list [5, 10, 13] would be 5).

23c. Provide at least three lists that would make good test cases for checking that your algorithm correctly identifies the mode.

Temporal Analysis Report

How much time did it require for your team to complete each Model?

Model 1:

Model 2:

Model 3:

Model 4: