Completing a full trace table for a dry run

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

Completing a full trace table for a dry run

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In which order do you fill in the rows of a trace table?
A One variable column at a time, from left to right
B In the order the algorithm runs, line by line
2. A variable changes value during the dry run. What do you do in its column?
A Cross out the old value and replace it in the same row
B Write the new value in the next row and keep the old values
3. When does an OUTPUT line get written in the OUTPUT column?
A On the row where the OUTPUT statement runs
B All together in the last row after the algorithm ends
4. The algorithm has OUTPUT "Enter a number" then INPUT Num. How do you record this?
A The prompt goes in the OUTPUT column and the typed value goes in the Num column
B Neither is recorded because the user is typing, not the program
5. The IF condition is false. What do you do in the trace table?
A Skip the THEN lines, so no values change inside them
B Run the THEN lines anyway and record the changes

Match the Pairs

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

Left columnRight column
1Total | Count | OUTPUT (column headings) AThe rows are out of order. The OUTPUT was written before the assignments that create the value had been traced.
2FOR Count ← 1 TO 3 (one new row each time a value changes) BFOR Num ← 1 TO 4 IF Num MOD 2 = 0 THEN Total ← Total + Num ENDIF NEXT Num
3Work through a loop with an IF: on each pass, test the condition and only update the variable when it is true CTotal ← 0 FOR Num ← 1 TO 3 Total ← Total + Num NEXT Num OUTPUT Total
4After the last row, check the final values: the last entry in each column is the final value of that variable and the OUTPUT column shows what was displayed DThe IF condition was not tested, so every Num was added to Total. Only the even values (2) should have changed Total.
5Num=1, Total=1 | Num=2, Total=3 | Num=3, Total=6 (for a loop with an IF that only adds even numbers) EFill in the rows in the same order the algorithm runs, so each loop pass adds new rows in sequence.
6Row 1: OUTPUT 6, Row 2: Total ← 0 (for an algorithm that sets Total to 0, then adds, then outputs) FThe trace table has one column for each variable and one for OUTPUT, so every change can be recorded.

Definitions

trace table

Fill in the Blanks

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

1.
FOR Count ← 1 TO 3 _____ ← Total + Count NEXT Count
2.
IF Number ___ 2 = 0 THEN OUTPUT "Even" ENDIF
3.
_____ Total < 10 DO Total ← Total + 4 ENDWHILE
4.
OUTPUT "Enter a number" _____ Number OUTPUT Number * 2
5.
FOR Count ← 1 TO 4 IF Count > 2 ____ OUTPUT Count NEXT Count

Spot the Error

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

Question 1
A Total ← 0
B FOR Count ← 1 UNTIL 3
C Total ← Total + Count
D NEXT Count
E OUTPUT Total
Question 2
A Count ← 0
B FOR N ← 1 TO 5
C IF N MOD 2 = 0
D Count ← Count + 1
E ENDIF
F NEXT N
Question 3
A Total ← 0
B Num ← INPUT
C WHILE Num <> 0 DO
D Total ← Total + Num
E INPUT Num
F ENDWHILE
Question 4
A DECLARE Total INTEGER
B Total ← 0
C FOR N ← 1 TO 4
D Total ← Total + N
E NEXT N
Question 5
A Count = 0
B REPEAT
C Count ← Count + 1
D UNTIL Count = 3
E OUTPUT Count

Answer Key

Completing a full trace table for a dry run
Section A: Quick Questions
Q1
B — In the order the algorithm runs, line by line
Q2
B — Write the new value in the next row and keep the old values
Q3
A — On the row where the OUTPUT statement runs
Q4
A — The prompt goes in the OUTPUT column and the typed value goes in the Num column
Q5
A — Skip the THEN lines, so no values change inside them
Section B: Match the Pairs
Q1
Total | Count | OUTPUT (column headings) → The trace table has one column for each variable and one for OUTPUT, so every change can be recorded.
Q2
FOR Count ← 1 TO 3 (one new row each time a value changes) → Fill in the rows in the same order the algorithm runs, so each loop pass adds new rows in sequence.
Q3
Work through a loop with an IF: on each pass, test the condition and only update the variable when it is true → FOR Num ← 1 TO 4 IF Num MOD 2 = 0 THEN Total ← Total + Num ENDIF NEXT Num
Q4
After the last row, check the final values: the last entry in each column is the final value of that variable and the OUTPUT column shows what was displayed → Total ← 0 FOR Num ← 1 TO 3 Total ← Total + Num NEXT Num OUTPUT Total
Q5
Num=1, Total=1 | Num=2, Total=3 | Num=3, Total=6 (for a loop with an IF that only adds even numbers) → The IF condition was not tested, so every Num was added to Total. Only the even values (2) should have changed Total.
Q6
Row 1: OUTPUT 6, Row 2: Total ← 0 (for an algorithm that sets Total to 0, then adds, then outputs) → The rows are out of order. The OUTPUT was written before the assignments that create the value had been traced.
Section C: Definitions
Q1
trace table — A table for a dry run: follow the algorithm line by line with the given inputs, writing each variable, output and prompt in the order it happens, then check the final values.
Section D: Fill in the Blanks
Q1
FOR Count ← 1 TO 3 Total ← Total + Count NEXT Count
Q2
IF Number MOD 2 = 0 THEN OUTPUT "Even" ENDIF
Q3
WHILE Total < 10 DO Total ← Total + 4 ENDWHILE
Q4
OUTPUT "Enter a number" INPUT Number OUTPUT Number * 2
Q5
FOR Count ← 1 TO 4 IF Count > 2 THEN OUTPUT Count NEXT Count
Section E: Spot the Error
Q1
B — A FOR loop uses TO to set its end value: FOR Count ← 1 TO 3. Without the correct header you cannot work out the rows of a trace table.
Q2
C — An IF condition must end with THEN. It should be IF N MOD 2 = 0 THEN, so you can trace which rows change Count.
Q3
B — Reading a value uses the INPUT command followed by the variable name: INPUT Num. In the trace table the input value goes in the Num column.
Q4
A — A declaration needs a colon between the identifier and the data type: DECLARE Total : INTEGER. Each declared variable gets a column in the trace table.
Q5
A — Assigning a starting value uses the assignment arrow: Count ← 0. The = sign is a comparison, not an assignment. The first trace table row shows Count as 0.