Completing a trace table for pseudocode

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Completing a trace table for pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. How many columns does a trace table need for the variables in an algorithm?
A One column for each variable
B One column for the whole algorithm
2. Where does a trace table show what the algorithm displays on screen?
A In an OUTPUT column
B In the column of the last variable
3. The question says the user types 7 and then 12. What do you do when you reach INPUT Num?
A Choose any numbers you like
B Use the values given in the question
4. Which statement should get a new row in a trace table?
A Total ← Total + 5
B // Add the numbers
5. Total is 10 and Count is 2. The statement Total ← Total + 4 runs. What do you write in the new row?
A Write 14 under Total and 14 under Count
B Write 14 under Total and leave Count blank

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1| Count | Total | OUTPUT | AThe header row of a trace table: one column for each variable (Count, Total) plus an OUTPUT column.
2INPUT Num (value given: 7) BTotal ← Total + Num
3Only write a new value in a column when that statement changes the variable CTake the next value supplied by the question and write it in the Num column on a new row.
4Anything printed by the algorithm is written in the OUTPUT column DThe OUTPUT column is missing, so there is nowhere to record what the algorithm displays.
5Trace table has columns for Count and Total only, but the algorithm uses OUTPUT Total EValues are copied into columns for variables that did not change on that statement.
6Row for Count ← Count + 1 also rewrites the Total and Num values, which did not change FOUTPUT Total

Definitions

trace table

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
Num | Total | ______
2.
Total ← 0 Total ← Total + 4 Row 2: Total = _
3.
_____ Score // the question supplies the value 60
4.
Count ← 3 OUTPUT Count OUTPUT row: Count unchanged = _
5.
// Total was 0, Num is 4, now Total is 4 Total ← Total _ Num

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Total ← 0
B FOR Count ← 1 TO 3
C Total ← Count + Count
D NEXT Count
E OUTPUT Total
Question 2
A input Num
B Total ← Num * 2
C OUTPUT Total
Question 3
A Total ← 10
B WHILE Total > 0 DO
C Total ← Total + 5
D ENDWHILE
E OUTPUT Total
Question 4
A Total = 0
B Num ← 4
C Total ← Total + Num
D OUTPUT Total
Question 5
A Num ← 3
B Num * 2 ← Num
C Num ← Num + 1
D OUTPUT Num

Answer Key

Completing a trace table for pseudocode
Section A: Quick Questions
Q1
A — One column for each variable
Q2
A — In an OUTPUT column
Q3
B — Use the values given in the question
Q4
A — Total ← Total + 5
Q5
B — Write 14 under Total and leave Count blank
Section B: Match the Pairs
Q1
| Count | Total | OUTPUT | → The header row of a trace table: one column for each variable (Count, Total) plus an OUTPUT column.
Q2
INPUT Num (value given: 7) → Take the next value supplied by the question and write it in the Num column on a new row.
Q3
Only write a new value in a column when that statement changes the variable → Total ← Total + Num
Q4
Anything printed by the algorithm is written in the OUTPUT column → OUTPUT Total
Q5
Trace table has columns for Count and Total only, but the algorithm uses OUTPUT Total → The OUTPUT column is missing, so there is nowhere to record what the algorithm displays.
Q6
Row for Count ← Count + 1 also rewrites the Total and Num values, which did not change → Values are copied into columns for variables that did not change on that statement.
Section C: Definitions
Q1
trace table — A table used to dry-run pseudocode: one column per variable plus OUTPUT, and a new row each time a statement changes a value. INPUT values come from the question.
Section D: Fill in the Blanks
Q1
Num | Total | OUTPUT
Q2
Total ← 0 Total ← Total + 4 Row 2: Total = 4
Q3
INPUT Score // the question supplies the value 60
Q4
Count ← 3 OUTPUT Count OUTPUT row: Count unchanged = 3
Q5
// Total was 0, Num is 4, now Total is 4 Total ← Total + Num
Section E: Spot the Error
Q1
C — Total must add Count to its own old value. Count + Count gives 2, 4, 6 in the Total column instead of 1, 3, 6. It should be Total ← Total + Count.
Q2
A — Pseudocode keywords must be in upper case. It should be INPUT Num. The value the question supplies is then written in the Num column of the trace table.
Q3
C — Total goes 15, 20, 25 and never reaches 0, so the trace table never finishes. It should be Total ← Total - 5, giving rows 10, 5, 0.
Q4
A — Assignment uses ←, not =. Without it Total is never given the value 0, so the first row of the Total column would be empty. It should be Total ← 0.
Q5
B — The variable being changed goes on the left of ←, and the calculation goes on the right. It should be Num ← Num * 2. The Num column gets a new value of 6 in this row.