# 8.5. Tracing Pseudocode Exercises¶

### Time Estimate: 45 minutes

## 8.5.1. Introduction and Goals¶

**Tracing** is a technique used to simulate a dry run through the code or pseudocode line by line by hand as if you are the computer executing the code. Tracing can be used for debugging or proving that your program runs correctly or for figuring out what the code actually does. **Trace tables** can be used to track the values of variables as they change throughout a program. To trace through code, write down a variable in each column of the trace table and keep track of its value throughout the program. Some trace tables also keep track of the output and the line number you are currently tracing.

## 8.5.2. Learning Activities¶

For example, here is a simple algorithm that adds x to the total every time through the loop which is executed 3 times.

x ← 2 total ← 0 REPEAT 3 TIMES { total ← total + x } DISPLAY(total)

Try tracing through the code and see if your results match the trace table below:

x | total | Output |
---|---|---|

2 | 0 | |

2 | ||

4 | ||

6 | 6 |

### POGIL Activity for the Classroom

Break into POGIL teams of 4 and assign each team member one of the following roles. Record your answers using this worksheet. (File-Make a Copy to have a version you can edit.)

Role | Responsibility |
---|---|

Facilitator | Reads the questions aloud, keeps track of time and makes sure everyone contributes appropriately. |

Spokesperson | Talks to the instructor and other teams. |

Quality Control | Records all answers & questions, and provides team reflection to team and instructor. |

Process Analyst | Considers how the team could work and learn more effectively. |

**Writing and Tracing Pseudocode: Critical Thinking Questions**

Suppose we have a list of numbers -- e.g., 5, 10, -2, -3, 7, 8, 12
Here's an algorithm that uses sequence, selection, and iteration (repetition) to add all
the ** even numbers** in the list and print out their sum.
Notice how indentation and curly brackets are used to clarify the different parts of the algorithm.

1 total ← 0 (Set total to 0) 2 FOR EACH number IN list 3 { 4 IF (EVEN(number)) 5 { 6 total ← total + number (Set total to the current total + number) 7 } 8 } 9 DISPLAY(total)

This algorithm contains examples of all three types of control structures, sequence, selection, and repetition. The lines are numbered for convenience.

- Which line(s) of the algorithm contain a repetition control structure? Remember a control structure can consist of multiple statements.
- Which line(s) of the algorithm contain a selection control structure?
- (
**Portfolio**) If you ran this algorithm on the list of numbers, 5, 10, -2, -3, 7, 8, 12, what would it print? When tracing through an algorithm, write down the variables (total and number) and pretend you are the computer executing each line of code and change the values of the variables on your paper as needed. - (
**Portfolio**) Suppose you had a list of positive numbers such as 5, 10, 12, 13, 6, 7, 1, 3, 2, 1. And suppose for each of the numbers in the list you added the number to a running total if it is even and subtracted it if it is odd, starting the total at 0. What result would you get for this list of numbers? - (
**Portfolio**) Write a pseudocode algorithm that implements the algorithm you used to calculate this total. Make sure that you use AP CSP text-style pseudocode.

## 8.5.3. Summary¶

In this lesson, you learned how to trace through code.

## 8.5.4. Self-Check¶

- Displays all the odd numbers between 1 and 100.
- Displays all the even numbers between 1 and 100.
- Displays all the numbers between 1 and 100.
- Displays 1.

Q-1: What does the following code do?

i ← 1 REPEAT UNTIL i >= 100 DISPLAY i i ← i + 2

## 8.5.5. Sample AP CSP Exam Question¶

- The number 0 is displayed.
- The number 6 is displayed.
- The number 10 is displayed.
- Nothing is displayed; the program results in an infinite loop.

Q-5: Consider the following program code. Which of the following best describes the result of running the program code?

## 8.5.6. Reflection: For Your Portfolio¶

Create a page named * Tracing Pseudocode* in your portfolio and answer the following questions:

- (
**POGIL**) If you ran the algorithm in the POGIL on the list of numbers, 5, 10, -2, -3, 7, 8, 12, what would it print? - (
**POGIL**) Suppose you had a list of positive numbers such as 5, 10, 12, 13, 6, 7, 1, 3, 2, 1. And suppose for each of the numbers in the list you added the number to a running total if it is even and subtracted it if it is odd. What result would you get for this list of numbers? - (
**POGIL**) Write a pseudocode algorithm that implements the algorithm you used to calculate this total. Make sure that you use AP CSP text-style pseudocode.