Tracing a 1D array program with a trace table

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

Tracing a 1D array program with a trace table

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which columns should a trace table for a FOR loop over a 1D array include?
A Only one column showing the final OUTPUT
B A column for Index and a column for each variable and array element being changed
2. When do you add a new row to a trace table?
A After each pass of the loop, to record the new values
B Only once, after the loop has finished
3. FOR Index ← 1 TO 3 then Scores[Index] ← Index * 2 then NEXT Index. After the loop, what is Scores[3]?
A 6
B 3
4. Nums holds 4, 5, 6. Total ← 0, then FOR i ← 1 TO 3: Total ← Total + Nums[i]. What is Total after the loop?
A 9
B 15
5. An array is declared ARRAY[1:5], but the loop is FOR Index ← 1 TO 6. What does the trace table show on the last row?
A Index = 6, which is outside the bounds 1 to 5 (off-by-one error)
B Index = 6, which is fine because the loop says TO 6

Match the Pairs

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

Left columnRight column
1Index | Total | Scores[Index] ADECLARE Nums : ARRAY[1:5] OF INTEGER FOR Index ← 1 TO 6 OUTPUT Nums[Index] NEXT Index
2Scores[3] ← 7 (row 3 of the table) BFOR Index ← 1 TO 3 Total ← Total + Nums[Index] NEXT Index OUTPUT Total
3Use a trace table to find the final OUTPUT by following the loop one pass at a time CThe trace table starts at the wrong index. The loop begins at 0, which is below the lower bound of 1, so the first row should show Index = 0 and an error
4A trace table shows an off-by-one error when Index goes past the upper bound of the array DColumn headings of a trace table: the loop variable, a running variable and the array element being used
5Trace table has only an Index column, with no column for Total or the array contents EThe table is missing columns for the variables that change, so you cannot see the array contents or the running total after each pass
6FOR Index ← 0 TO 4 on ARRAY[1:5], trace starts with Index = 1 FRecord the new array contents after the pass where Index is 3

Definitions

trace table

Fill in the Blanks

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

1.
DECLARE Score : ARRAY[1:3] OF INTEGER FOR Index ← 1 __ 3 OUTPUT Score[Index]
2.
FOR Index ← 1 TO 3 Total ← Total + Score[_____] NEXT Index
3.
DECLARE Mark : ARRAY[1:5] OF INTEGER FOR Index ← 1 TO _ OUTPUT Mark[Index]
4.
DECLARE Mark : ARRAY[0:2] OF INTEGER FOR Index ← _ TO 2 Mark[Index] ← Index * 2
5.
Total ← _ Total ← Total + Score[1] OUTPUT Total

Spot the Error

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

Question 1
A Total ← 0
B FOR Index ← 1 TO 3
C Total ← Total + Score[Total]
D NEXT Index
E OUTPUT Total
Question 2
A FOR i ← 1 TO 4
B Num[i] ← i * 2
C NEXT i
D OUTPUT Num[5]
Question 3
A // Temp has indexes 1 to 5
B FOR Day ← 0 TO 5
C Temp[Day] ← Day * 10
D NEXT Day
E OUTPUT Temp[5]
Question 4
A Total ← 1
B FOR i ← 1 TO 3
C Total ← Total + Data[i]
D NEXT i
E OUTPUT Total
Question 5
A Big ← Data[1] // Data[1:4]
B FOR i ← 2 TO 3
C IF Data[i] > Big THEN
D Big ← Data[i]
E ENDIF
F NEXT i

Answer Key

Tracing a 1D array program with a trace table
Section A: Quick Questions
Q1
B — A column for Index and a column for each variable and array element being changed
Q2
A — After each pass of the loop, to record the new values
Q3
A — 6
Q4
B — 15
Q5
A — Index = 6, which is outside the bounds 1 to 5 (off-by-one error)
Section B: Match the Pairs
Q1
Index | Total | Scores[Index] → Column headings of a trace table: the loop variable, a running variable and the array element being used
Q2
Scores[3] ← 7 (row 3 of the table) → Record the new array contents after the pass where Index is 3
Q3
Use a trace table to find the final OUTPUT by following the loop one pass at a time → FOR Index ← 1 TO 3 Total ← Total + Nums[Index] NEXT Index OUTPUT Total
Q4
A trace table shows an off-by-one error when Index goes past the upper bound of the array → DECLARE Nums : ARRAY[1:5] OF INTEGER FOR Index ← 1 TO 6 OUTPUT Nums[Index] NEXT Index
Q5
Trace table has only an Index column, with no column for Total or the array contents → The table is missing columns for the variables that change, so you cannot see the array contents or the running total after each pass
Q6
FOR Index ← 0 TO 4 on ARRAY[1:5], trace starts with Index = 1 → The trace table starts at the wrong index. The loop begins at 0, which is below the lower bound of 1, so the first row should show Index = 0 and an error
Section C: Definitions
Q1
trace table — A table that records the loop variable, index and array contents after each pass of a loop, so you can predict the final OUTPUT and spot errors such as off-by-one.
Section D: Fill in the Blanks
Q1
DECLARE Score : ARRAY[1:3] OF INTEGER FOR Index ← 1 TO 3 OUTPUT Score[Index]
Q2
FOR Index ← 1 TO 3 Total ← Total + Score[Index] NEXT Index
Q3
DECLARE Mark : ARRAY[1:5] OF INTEGER FOR Index ← 1 TO 5 OUTPUT Mark[Index]
Q4
DECLARE Mark : ARRAY[0:2] OF INTEGER FOR Index ← 0 TO 2 Mark[Index] ← Index * 2
Q5
Total ← 0 Total ← Total + Score[1] OUTPUT Total
Section E: Spot the Error
Q1
C — The index must be the loop variable Index. Using Total as the index means the trace table shows the wrong elements being read. It should be Score[Index].
Q2
D — The trace table only fills Num[1] to Num[4]. Num[5] is outside the array (or never set). It should be OUTPUT Num[4], which gives 8.
Q3
B — This is an off-by-one error. The first row of the trace table would use Day = 0, but Temp[0] does not exist. The loop should start at 1.
Q4
A — The Total column in the trace table must start at 0. Starting at 1 makes every row, and the final OUTPUT, too big by 1. It should be Total ← 0.
Q5
B — This is an off-by-one error. The loop stops at 3, so the trace table never checks Data[4]. The upper limit should be 4.