Using a trace table to find an error

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

Using a trace table to find an error

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A trace table shows Total is 3 in row 4, but you expected 5. How do you locate the faulty line?
A Find the first row where a value differs from what you expected, then check the line that set it
B Look only at the final row and guess which line is wrong
2. An algorithm should output Pass when Mark is 50 or more. The trace table shows Mark = 50 gives Fail. The condition is IF Mark > 50 THEN. Which fix is correct?
A Change > to >= so that 50 passes
B Change 50 to 500 in the condition
3. A trace table for a counting algorithm shows Count is 4 after 3 values were entered. Count was never set before the loop. What is the likely error?
A Count was not set to 0 before the loop
B The OUTPUT column has too many rows
4. Count should be 5 at the end of a loop that counts five values. The trace table shows Count = 4 in the last row. Which cause matches this?
A The loop ran one pass too many
B The loop ended one pass too soon
5. A trace table shows the first wrong value is in row 2 and every row after it is also wrong. Where should you look first?
A At the line that produced the value in the last row
B At the line that produced the value in row 2

Match the Pairs

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

Left columnRight column
1IF Mark > 50 THEN OUTPUT "Pass" AFOR Count ← 1 TO 5
2Total ← Total + Score (Total was never set to 0) BThe wrong operator makes the loop stop one pass too soon. The trace table shows no row for Number = 10. It should be <= instead of <.
3A loop must run 5 times, but the trace table shows only 4 passes. The first wrong row points to the loop header. Correction: change the end value to 5. CThe first row of the trace table already shows a wrong Total, so the error is a missing starting value. Fix it by adding Total ← 0 before the loop.
4The trace table shows Count is 1 higher than expected on every row, so the error is a counter not set to 0. Correction: set the counter to 0 before the loop. DThe loop ends one pass too soon, so the last letter is never traced. The correction is to remove the - 1 from the end value.
5WHILE Number < 10 DO (should include 10) EA wrong operator: a mark of 50 should pass but the trace table shows no Pass. Fix it by changing > to >=.
6FOR Index ← 1 TO LENGTH(Word) - 1 FCount ← 0

Definitions

first wrong value

Fill in the Blanks

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

1.
// 50 or more is a Pass IF Mark __ 50 THEN OUTPUT "Pass"
2.
Count ← _ FOR Index ← 1 TO 5 Count ← Count + 1
3.
// Output 1 to 5 FOR Index ← 1 TO _ OUTPUT Index
4.
// Add each mark to the total Total ← Total + ____ Count ← Count + 1
5.
// Only under 18 is a Child IF Age _ 18 THEN OUTPUT "Child"

Spot the Error

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

Question 1
A Count ← 0
B FOR Index ← 1 TO 4
C Count ← Count + 1
D NEXT Index
E OUTPUT Count // should be 5
Question 2
A INPUT Mark
B Pass ← FALSE
C IF Mark > 50 THEN
D Pass ← TRUE
E ENDIF
Question 3
A DECLARE Count : INTEGER
B Total ← 0
C Count ← 5
D FOR Index ← 1 TO 4
E Count ← Count + 1
F NEXT Index
Question 4
A Total ← 10
B FOR Index ← 1 TO 3
C INPUT Mark
D Total ← Total + Mark
E OUTPUT Total
Question 5
A Num ← 1
B Total ← 0
C WHILE Num <= 4 DO
D Total ← Total + Num
E Num ← Num + 1
F ENDWHILE

Answer Key

Using a trace table to find an error
Section A: Quick Questions
Q1
A — Find the first row where a value differs from what you expected, then check the line that set it
Q2
A — Change > to >= so that 50 passes
Q3
A — Count was not set to 0 before the loop
Q4
B — The loop ended one pass too soon
Q5
B — At the line that produced the value in row 2
Section B: Match the Pairs
Q1
IF Mark > 50 THEN OUTPUT "Pass" → A wrong operator: a mark of 50 should pass but the trace table shows no Pass. Fix it by changing > to >=.
Q2
Total ← Total + Score (Total was never set to 0) → The first row of the trace table already shows a wrong Total, so the error is a missing starting value. Fix it by adding Total ← 0 before the loop.
Q3
A loop must run 5 times, but the trace table shows only 4 passes. The first wrong row points to the loop header. Correction: change the end value to 5. → FOR Count ← 1 TO 5
Q4
The trace table shows Count is 1 higher than expected on every row, so the error is a counter not set to 0. Correction: set the counter to 0 before the loop. → Count ← 0
Q5
WHILE Number < 10 DO (should include 10) → The wrong operator makes the loop stop one pass too soon. The trace table shows no row for Number = 10. It should be <= instead of <.
Q6
FOR Index ← 1 TO LENGTH(Word) - 1 → The loop ends one pass too soon, so the last letter is never traced. The correction is to remove the - 1 from the end value.
Section C: Definitions
Q1
first wrong value — In a trace table, the first row where a value differs from what you expected points to the faulty line of the algorithm. Look at that line, then suggest a correction.
Section D: Fill in the Blanks
Q1
// 50 or more is a Pass IF Mark >= 50 THEN OUTPUT "Pass"
Q2
Count ← 0 FOR Index ← 1 TO 5 Count ← Count + 1
Q3
// Output 1 to 5 FOR Index ← 1 TO 5 OUTPUT Index
Q4
// Add each mark to the total Total ← Total + Mark Count ← Count + 1
Q5
// Only under 18 is a Child IF Age < 18 THEN OUTPUT "Child"
Section E: Spot the Error
Q1
B — The loop ends one pass too soon. The trace table stops with Count = 4 when 5 was expected, and the last row points back to the FOR line. It should be FOR Index ← 1 TO 5.
Q2
C — Using > instead of >= gives the wrong result for 50. When the trace table is filled in with a Mark of 50, Pass stays FALSE instead of becoming TRUE. The condition should be IF Mark >= 50 THEN.
Q3
C — The counter does not start at 0. The first row of the trace table shows Count = 5 where 0 was expected, which points to this line. It should be Count ← 0.
Q4
A — The total is not set to 0 before the loop. The first row of the trace table shows Total = 10 instead of 0, so every later value is 10 too high. It should be Total ← 0.
Q5
C — The loop ends one pass too soon. The trace table has no row for Num = 5, so Total ends at 10 instead of 15. The condition should be WHILE Num <= 5 DO.