Year 10 · Computer Science · Spring 2

Algorithm design and problem solving · Lesson 5 of 5

Trace tables and finding errors

Exam practice7.7 Trace tables7.8 Finding and fixing errors

Year 10 · Computer Science · Algorithm design and problem solving · Lesson 5 of 5

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.

Year 10 · Computer Science · Algorithm design and problem solving · Lesson 5 of 5

Success criteria

You have got it when you can say:

  • 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.

Year 10 · Computer Science · Algorithm design and problem solving · Lesson 5 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last lesson

    A diamond in a flowchart means…

    A A processB A decisionC Input or output

    B. A decision

  2. Last unit

    Which memory is volatile?

    A RAMB An SSDC ROM

    A. RAM

  3. Earlier this year

    In 8-bit two's complement, 11111111 is…

    A −127B −1C 255

    B. −1

Year 10 · Computer Science · Algorithm design and problem solving · Lesson 5 of 5

Key words (1 of 2)

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.

Year 10 · Computer Science · Algorithm design and problem solving · Lesson 5 of 5

Key words (2 of 2)

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.

7.7 Trace tables · The explanation · step 1 of 5 · part 1 of 2

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.

7.7 Trace tables · The explanation · step 1 of 5 · part 2 of 2

What a trace table is (continued)

!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.

7.7 Trace tables · The explanation · step 2 of 5 · part 1 of 6

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.

7.7 Trace tables · The explanation · step 2 of 5 · part 2 of 6

Worked dry run (continued)

ValueTotalCountHighestOUTPUT
000
1212112
7192
2039320
9484
-112 20

7.7 Trace tables · The explanation · step 2 of 5 · part 3 of 6

Worked dry run (continued)

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.

7.7 Trace tables · The explanation · step 2 of 5 · part 4 of 6

Worked dry run (continued)

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

7.7 Trace tables · The explanation · step 2 of 5 · part 5 of 6

Worked dry run (continued)

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)

7.7 Trace tables · The explanation · step 2 of 5 · part 6 of 6

Worked dry run (continued)

!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 · The explanation · step 3 of 5 · part 1 of 2

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.

7.8 Finding and fixing errors · The explanation · step 3 of 5 · part 2 of 2

Two kinds of error (continued)

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.

The explanation · 2 more steps

More on the lesson page

The lesson page has the rest of the explanation, step by step:

  • Worked example: find four errors
  • The corrected algorithm

Open the lesson page

Year 10 · Computer Science · Algorithm design and problem solving · Lesson 5 of 5

Quick quiz · 1 of 3

Bubble sort stops when…

  1. A Half the list is sorted
  2. B The first swap happens
  3. C A whole pass makes no swaps

C. A whole pass makes no swaps

No swaps in a pass means everything is in order.

Year 10 · Computer Science · Algorithm design and problem solving · Lesson 5 of 5

Quick quiz · 2 of 3

The four stages of the program development life cycle are…

  1. A Fetch, decode, execute, store
  2. B Analysis, design, coding, testing
  3. C Input, process, output, storage

B. Analysis, design, coding, testing

Analyse the problem, design the solution, write the code, test it.

Year 10 · Computer Science · Algorithm design and problem solving · Lesson 5 of 5

Quick quiz · 3 of 3

Abnormal test data is…

  1. A Data that should be rejected
  2. B Data at the edge of the range
  3. C Typical data

A. Data that should be rejected

Normal works, abnormal is rejected, extreme sits on the edge, boundary is either side of it.

Year 10 · Computer Science · Algorithm design and problem solving · Lesson 5 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. A range check on age 0 to 120 would reject…

    A 150B 0C 120

    A. 150 0 and 120 are the edges and are allowed. 150 is outside.

  2. Typing an email address twice to check it is…

    A A trace tableB VerificationC Validation

    B. Verification Validation checks the data is sensible; verification checks it was entered as intended.

Year 10 · Computer Science · Algorithm design and problem solving · Lesson 5 of 5

Homework and the challenge

Homework

The CS 7 quiz; 'Trace a loop' and 'Normal / boundary / erroneous' practice on the study pages.

End-of-unit challenge

End-of-unit test: past-paper questions on topic 7 (trace tables, pseudocode, test data).