Tracing Pseudocode Exercises

Time Estimate: 45 minutes

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.

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:

xtotalOutput
20
2
4
66

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

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

  1. Which line(s) of the algorithm contain a repetition control structure? Remember a control structure can consist of multiple statements.
  2. Which line(s) of the algorithm contain a selection control structure?
  3. (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.
  4. (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?
  5. (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.

Summary

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

Self-Check

Sample AP CSP Exam Question

Reflection: For Your Portfolio

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

  1. (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?
  2. (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?
  3. (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.
You have attempted of activities on this page