Trace Table Exercises
Unit 4 - Testing & Debugging
Instructions
For each algorithm, complete the trace table showing how variable values change at each step. Write the final output at the end of each question.
Question 1: Simple Counter (2 marks)
count = 0
WHILE count < 3
count = count + 1
OUTPUT count
ENDWHILE Complete the trace table:
| Step | count | count < 3? | OUTPUT |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 |
Final Output:
Question 2: Sum Calculator (3 marks)
total = 0
FOR i = 1 TO 4
total = total + i
NEXT i
OUTPUT total Complete the trace table:
| Step | i | total |
|---|---|---|
| Initial | ||
| Loop 1 | ||
| Loop 2 | ||
| Loop 3 | ||
| Loop 4 |
Final Output:
Question 3: Maximum Finder (4 marks)
numbers = [5, 2, 8, 3]
max = numbers[0]
FOR i = 1 TO 3
IF numbers[i] > max THEN
max = numbers[i]
ENDIF
NEXT i
OUTPUT max Complete the trace table:
| Step | i | numbers[i] | numbers[i] > max? | max |
|---|---|---|---|---|
| Initial | - | - | - | |
| Loop 1 | ||||
| Loop 2 | ||||
| Loop 3 |
Final Output:
Question 4: Password Checker (4 marks)
password = "secret"
attempts = 0
valid = FALSE
REPEAT
INPUT guess
attempts = attempts + 1
IF guess = password THEN
valid = TRUE
ENDIF
UNTIL valid = TRUE OR attempts = 3
IF valid THEN
OUTPUT "Access granted"
ELSE
OUTPUT "Locked out"
ENDIF Complete the trace table for inputs: "wrong", "wrong", "secret"
| Step | guess | attempts | valid | Loop ends? |
|---|---|---|---|---|
| Initial | - | - | ||
| Attempt 1 | ||||
| Attempt 2 | ||||
| Attempt 3 |
Final Output:
Question 5: Grade Calculator (5 marks)
scores = [85, 72, 90, 68]
total = 0
FOR i = 0 TO 3
total = total + scores[i]
NEXT i
average = total / 4
IF average >= 80 THEN
grade = "A"
ELSE IF average >= 70 THEN
grade = "B"
ELSE IF average >= 60 THEN
grade = "C"
ELSE
grade = "D"
ENDIF
OUTPUT grade Complete the trace table:
| Step | i | scores[i] | total | average | grade |
|---|---|---|---|---|---|
| Initial | - | - | - | - | |
| Loop 1 | - | - | |||
| Loop 2 | - | - | |||
| Loop 3 | - | - | |||
| Loop 4 | - | - | |||
| Calc Avg | - | - | - | ||
| Assign Grade | - | - |
Final Output:
View Answer Key (for self-marking)
Q1: Output is 1, 2, 3. Trace: count goes 0, 1, 2, 3, then exits when condition is false
Q2: Output is 10. Total: 0, 1, 3, 6, 10 (sum of 1+2+3+4)
Q3: Output is 8. Max starts at 5, stays 5 for 2, becomes 8 for 8, stays 8 for 3
Q4: Output is "Access granted". Loop runs 3 times, valid becomes TRUE on 3rd attempt
Q5: Output is "B". Total=315, average=78.75, grade is B (between 70 and 80)