Teachers' Portal  /  IGCSE Computer Science 0478  /  Algorithm design and problem-solving  /  Dry-running algorithms with trace tables
Lesson pack · IGCSE Computer Science 0478 · 7 (7-9) · IGCSE

Dry-running algorithms with trace tables

Be the computer. A dry run means working through an algorithm by hand, one line at a time, exactly as the computer would.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Algorithm design and problem-solving
Syllabus reference
Cambridge IGCSE Computer Science 0478, 7 (7-9)
Level
IGCSE
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 7 of 9 in Algorithm design and problem-solving; follows Normal, abnormal, extreme and boundary test data; leads into Finding and correcting errors in algorithms
Central model
Fig. 1: a completed trace table for the digit-sum algorithm with input 352
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • Filling in a trace table: Load a program in the lab, enter the test data and step through it line by line.
  • Loops and selection: With a FOR loop, the loop counter gets its own column.
  • Post-condition loops and DIV/MOD: This algorithm adds up the digits of a whole number.
Key points to leave on the board
  • Columns: one per variable, plus OUTPUT (prompts too).
  • Rows: move down as the algorithm moves on; record a value only when it changes.
  • Conditions: test each one with the current values, every time.
  • Loops: WHILE tests first (may run 0 times); REPEAT tests last (runs at least once).
The core visual model

One picture every idea returns to

A dry run is working through an algorithm by hand with given test data. A trace table has a column for each variable and one for OUTPUT (including user prompts). Work line by line: write a new value in the column only when it changes, move down a row as the algorithm moves on, and follow every condition and loop exactly as written, not as you think the programmer meant.

Trace table for the digit-sum algorithm, input 352 Number Digit Sum OUTPUT 352 0 35 2 2 3 5 7 0 3 10 10 one new row each time round the loop; the highlighted row is the third pass
Fig. 1: a completed trace table for the digit-sum algorithm with input 352
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Be the computer.. A dry run means working through an algorithm by hand, one line at a time, exactly as the computer would. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: Filling in a trace tableLoad a program in the lab, enter the test data and step through it line by line. Project tab 1 of the lab (Programs with a live trace table) and let a student drive it while the class predicts.Whole class, then pairs
13 to 21 minTeach and check: Loops and selectionWith a FOR loop, the loop counter gets its own column. Model the worked example Trace with input 6 on the board, one line at a time. Your turn: Dry-run the same algorithm with the input 7. What are the four outputs?Pairs
21 to 30 minTeach and check: Post-condition loops and DIV/MODThis algorithm adds up the digits of a whole number. Model the worked example Input 4071 on the board, one line at a time. Your turn: What is output for the input 9005, and how many times does the loop run?Round Table
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 18 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in Post-condition loops and DIV/MOD, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Round Table

One sheet travels round the team. Each student adds one line of working or one idea and passes it on, so the solution is built four hands at a time and every contribution is visible.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
The rogue value -1 is input and recorded in the trace table, but the loop ends before it is processed.Do not add it to the total.
Use the new value of a variable on the next line.After Number becomes 3, the OUTPUT on the same pass shows 3, not 6.
A REPEAT loop always runs at least once, because the condition is tested at the end.With input 0 this algorithm still runs once and outputs 0.
Worksheet

Dry-running algorithms with trace tables: worksheet

6 questions, 18 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · trace[4 marks]

Complete a trace table for this algorithm.

A ← 1
B ← 1
FOR I ← 1 TO 4
    C ← A + B
    A ← B
    B ← C
    OUTPUT C
NEXT I
Q2 · trace[3 marks]

Dry-run the digit-sum algorithm from this lesson with the input 608. Give the values of Digit, Sum and Number after each pass, and the output.

Q3 · trace[4 marks]

Complete a trace table for this algorithm with the inputs 15, 40, 25, 0.

Max ← 0
Total ← 0
INPUT Value
WHILE Value <> 0 DO
    Total ← Total + Value
    IF Value > Max
      THEN
        Max ← Value
    ENDIF
    INPUT Value
ENDWHILE
OUTPUT Total, " ", Max
Q4 · calculate[2 marks]

State the values of DIV(29, 4) and MOD(29, 4).

Q5 · trace[3 marks]

Dry-run this algorithm and state each output.

Count ← 10
REPEAT
    IF Count > 5
      THEN
        Count ← Count - 3
      ELSE
        Count ← Count - 1
    ENDIF
    OUTPUT Count
UNTIL Count <= 2
Q6 · explain[2 marks]

Explain why a trace table is useful when testing an algorithm.

Answer key and marking notes
Q1 · 4 marks
  • I = 1: C = 2, A = 1, B = 2, output 2.
  • I = 2: C = 3, A = 2, B = 3, output 3.
  • I = 3: C = 5, A = 3, B = 5, output 5.
  • I = 4: C = 8, A = 5, B = 8, output 8.
Q2 · 3 marks
  • Pass 1: Digit 8, Sum 8, Number 60.
  • Pass 2: Digit 0, Sum 8, Number 6; Pass 3: Digit 6, Sum 14, Number 0.
  • Output 14.
Q3 · 4 marks
  • Value 15: Total 15, Max 15.
  • Value 40: Total 55, Max 40.
  • Value 25: Total 80 (Max stays 40).
  • Value 0 ends the loop; output 80 40.
Q4 · 2 marks
  • DIV(29, 4) = 7.
  • MOD(29, 4) = 1.
Q5 · 3 marks
  • 10 > 5: Count 7, output 7. 7 > 5: Count 4, output 4.
  • 4 is not > 5: Count 3, output 3.
  • 3 is not > 5: Count 2, output 2; 2 <= 2 so the loop stops. Outputs: 7, 4, 3, 2.
Q6 · 2 marks
  • It shows the value of each variable (and each output) at every step of the algorithm.
  • So the actual results can be compared with the expected results, and the line where an error happens can be found.
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

What are the values of DIV(58, 8) and MOD(58, 8)?

  • A7 and 2
  • B2 and 7
  • C7.25 and 2
  • D7 and 0.25
Q21 mark

What is output by this algorithm?

X ← 3
Y ← 0
FOR I ← 1 TO 4
    Y ← Y + X
    X ← X - 1
NEXT I
OUTPUT X, " ", Y

  • A-1 6
  • B0 6
  • C-1 10
  • D-1 2
Q31 mark

The inputs are 60, 50, 75, 0, 90. What is output?

Count ← 0
INPUT Number
WHILE Number > 0 DO
    IF Number > 50
      THEN
        Count ← Count + 1
    ENDIF
    INPUT Number
ENDWHILE
OUTPUT Count

  • A2
  • B3
  • C4
  • D1
Q41 mark

What is output by this algorithm?

N ← 20
REPEAT
    N ← N - 7
    OUTPUT N
UNTIL N < 5

  • A13, 6, -1
  • B13, 6
  • C20, 13, 6
  • D13, 6, -1, -8
Q51 mark

The input is 3070. What is output?

INPUT Number
Result ← 0
WHILE Number > 0 DO
    Result ← Result * 10 + MOD(Number, 10)
    Number ← DIV(Number, 10)
ENDWHILE
OUTPUT Result

  • A703
  • B3070
  • C7030
  • D73
Q61 mark

What is output by this algorithm?

A ← 0
B ← 1
FOR I ← 1 TO 5
    IF MOD(I, 2) = 0
      THEN
        A ← A + I
      ELSE
        B ← B * I
    ENDIF
NEXT I
OUTPUT A, " ", B

  • A6 15
  • B9 8
  • C15 6
  • D6 0
Quiz key
Q1: A

58 = 7 × 8 + 2, so DIV(58, 8) = 7 and MOD(58, 8) = 2. Swapping the two mixes up quotient and remainder. 7.25 is the ordinary division 58 / 8, which DIV does not give. The remainder is a whole number, 2, not the decimal part 0.25.

Q2: A

Y gains 3, 2, 1 and 0 (Y = 3, 5, 6, 6) while X goes 2, 1, 0, -1, so the output is -1 6. "0 6" stops reducing X one pass too early. "-1 10" adds 4 + 3 + 2 + 1, as if X started at 4. "-1 2" reduces X before adding it to Y, the reverse of the order of the two lines.

Q3: A

60 and 75 are greater than 50; 50 is not (the test is > 50, not >= 50). The input 0 makes Number > 0 false, so the loop ends and 90 is never read: Count = 2. 3 counts either 50 or 90. 4 counts both. 1 stops after the first input.

Q4: A

Pass 1: N = 13, output 13, 13 < 5 false. Pass 2: N = 6, output 6, false. Pass 3: N = -1, output -1, -1 < 5 true, so the loop stops. Stopping at 6 tests the condition before the output, as if it were a WHILE loop. 20 is never output because N is reduced first. -8 would need a fourth pass after the condition was already true.

Q5: A

Pass 1: digit 0, Result 0, Number 307. Pass 2: digit 7, Result 7, Number 30. Pass 3: digit 0, Result 70, Number 3. Pass 4: digit 3, Result 703, Number 0, so the loop ends. The algorithm reverses the digits, and the leading zero of 0703 is not kept in an integer, so 703 is output. 3070 is the input unchanged. 7030 swaps pairs of digits instead of reversing. 73 ignores the 0 digits, but Result is still multiplied by 10 on the pass where the digit is 0.

Q6: A

Even I (2 and 4) are added to A: A = 6. Odd I (1, 3, 5) multiply B: B = 1 × 1 × 3 × 5 = 15. Output 6 15. "9 8" sends odd values to A and even to B, reversing the condition. "15 6" outputs the values in the wrong order. "6 0" assumes B starts at 0, but it starts at 1, so the products are not wiped out.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonNormal, abnormal, extreme and boundary test data