Skip to the page
Year 10 · Computer Science · Spring 2 · Algorithm design and problem solving

Trace tables and finding errors

Exam practicePresent ▶

What this lesson covers

7.7 Trace tables

Cambridge IGCSE Computer Science 0478 · Topic 7: Algorithm design and problem-solving · Paper 2

7.8 Finding and fixing errors

Cambridge IGCSE Computer Science 0478 · Topic 7: Algorithm design and problem-solving · Paper 2

You will learn to

  • Explain what a dry run and a trace table are for.
  • Complete a trace table for an algorithm with a loop and selection.
  • Identify syntax errors and logic errors in an algorithm, giving the line number.
  • Write a correct replacement line for each error.

Success criteria

  • I can complete five past-paper trace tables under time.
  • I can find all three planted errors and state each line number.
  • I can rewrite each wrong line so the algorithm works.
The syllabus points for these sections
  • Explain what a dry run and a trace table are for.
  • Complete a trace table for an algorithm with a loop and selection.
  • Use a trace table to find the output and spot errors.
  • Tell the difference between syntax errors and logic errors.
  • Find errors in a given algorithm, stating the line number.
  • Write a correct replacement line for each error.

Key words

Say each word, then read what it means. Every one is in the glossary too.

Algorithm
A precise, ordered set of steps that solves a problem.
Program development life cycle
The four stages of making a program: analysis, design, coding and testing.
Pseudocode
A structured, code-like way of writing an algorithm that is not tied to one language.
Counting
Adding 1 to a counter each time something happens.
Check digit
An extra digit calculated from the other digits, used to catch typing errors.
Trace table
A table used in a dry run to record the value of each variable as an algorithm runs.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

7.7 Trace tables

What a trace table is

A dry run means working through an algorithm by hand, line by line, without a computer.

A trace table records the dry run. It has one column for each variable, and one for the output.

Write a new value in a column only when that variable changes. Move to a new row when the loop goes round again.

?Why?

A dry run lets you check an algorithm before it is coded. It also shows exactly where a logic error happens.

!Watch out

Do not fill every cell. Cambridge mark schemes usually want a value only where it changes. Copying values down is not wrong but takes time and causes mistakes.

Worked dry run

This algorithm reads numbers until the user types -1. A value that only means 'stop' is called a rogue value.

Input data: 12, 7, 20, 9, -1. Follow each row of the table below and check you agree.

Then use the toy. Pick Counting (WHILE), a similar rogue-value loop, and press Step to run it one line at a time.

ValueTotalCountHighestOUTPUT
000
1212112
7192
2039320
9484
-112 20
e.g.Example
Row 1: the three variables start at 0. Value 12: Total 0 + 12 = 12, Count 1. 12 > 0, so Highest = 12. Value 7: Total 19, Count 2. 7 > 12 is false, so Highest stays 12. Value 20: Total 39, Count 3. 20 > 12, so Highest = 20. Value 9: Total 48, Count 4. 9 > 20 is false. Value -1: the WHILE condition is false, so the loop ends. Output: 48 / 4 = 12, then 20.
DECLARE Value : INTEGER
DECLARE Total : INTEGER
DECLARE Count : INTEGER
DECLARE Highest : INTEGER
Total ← 0
Count ← 0
Highest ← 0
INPUT Value
WHILE Value <> -1 DO
  Total ← Total + Value
  Count ← Count + 1
  IF Value > Highest
    THEN
      Highest ← Value
  ENDIF
  INPUT Value
ENDWHILE
OUTPUT Total / Count, " ", Highest
total = 0
count = 0
highest = 0
value = int(input())
while value != -1:
    total = total + value
    count = count + 1
    if value > highest:
        highest = value
    value = int(input())
print(total / count, highest)
!Watch out

The rogue value -1 is not added to the total. The loop checks the value before using it. That is why there is an `INPUT` before the loop and another at the end of it.

There is something to try for this step in the IGCSE library.

7.8 Finding and fixing errors

Two kinds of error

A syntax error breaks the rules of the language, like a spelling mistake in a keyword. The program will not run.

A logic error means the program runs but gives the wrong answer. Examples: `<` instead of `>`, or a total that starts at 1.

Exam questions mostly test logic errors. Trace tables and test data are how you find them.

ErrorExampleWhat happens
Syntax`OUTPT Total`Translator reports an error; program does not run
Logic`Count ← 1` instead of `0`Runs, but the count is 1 too high
Logic`FOR Index ← 1 TO 9` for 10 itemsRuns, but misses the last item
?Why?

A translator finds syntax errors for you. Nothing finds logic errors automatically, so you need to think and test.

Worked example: find four errors

This algorithm should input 10 numbers, output their average, and count how many are over 50. It has four errors.

Work through it line by line. For each line ask: does this do what the purpose says?

LineErrorCorrection
06Count starts at 1`Count ← 0`
09Total is replaced, not added to`Total ← Total + Number`
10Counts numbers under 50`IF Number > 50`
15Divides by the loop counter`OUTPUT "Average: ", Total / 10`
e.g.Example
Test the corrected algorithm with 40, 55, 60, 12, 51, 50, 70, 8, 90, 64. Total = 500. Average = 500 / 10 = 50. Over 50: 55, 60, 51, 70, 90, 64 → 6. (50 is not over 50.)
01 DECLARE Total : INTEGER
02 DECLARE Count : INTEGER
03 DECLARE Number : INTEGER
04 DECLARE Index : INTEGER
05 Total ← 0
06 Count ← 1
07 FOR Index ← 1 TO 10
08   INPUT Number
09   Total ← Number
10   IF Number < 50
11     THEN
12       Count ← Count + 1
13   ENDIF
14 NEXT Index
15 OUTPUT "Average: ", Total / Index
16 OUTPUT "Numbers over 50: ", Count
!Watch out

In the exam, give the line number and the full corrected line. 'Line 9 is wrong' earns nothing on its own.

The corrected algorithm

Here is the fixed version in pseudocode and in Python. Run the Python with the test data above to check it.

After a fix, test again. A fix in one place can cause a new error somewhere else.

e.g.Example
Output: Average: 50.0 Numbers over 50: 6
DECLARE Total : INTEGER
DECLARE Count : INTEGER
DECLARE Number : INTEGER
DECLARE Index : INTEGER
Total ← 0
Count ← 0
FOR Index ← 1 TO 10
  INPUT Number
  Total ← Total + Number
  IF Number > 50
    THEN
      Count ← Count + 1
  ENDIF
NEXT Index
OUTPUT "Average: ", Total / 10
OUTPUT "Numbers over 50: ", Count
total = 0
count = 0
for index in range(10):
    number = int(input())
    total = total + number
    if number > 50:
        count = count + 1
print("Average:", total / 10)
print("Numbers over 50:", count)
?Why?

In Cambridge pseudocode you should not rely on the value of the loop counter after `NEXT`. Dividing by the fixed count, 10, is always safe.

Study it

Open the study page and go one step at a time.

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.