Course 7.2Lesson 2 of 3
A dry run follows an algorithm by hand, one step at a time, without a computer. A trace table records it: one column for each variable, and columns for the prompts and outputs. You complete trace tables for loops and decisions and use them to find what an algorithm does.
Try the code and the simulations beside the lesson.
How can you be sure an algorithm works before it is a program? You dry-run it: you play the part of the computer, carry out each line in order, and write down every value. A trace table is the table you write it in. The syllabus asks you to record, at each step, the variables, the outputs and the user prompts. A user prompt is a message that asks the user to type something.
Here is an algorithm that counts the even numbers among four inputs. The inputs are , , and .
Evens ← 0
FOR Index ← 1 TO 4
OUTPUT "Enter a number"
INPUT Value
IF MOD(Value, 2) = 0
THEN
Evens ← Evens + 1
ENDIF
NEXT Index
OUTPUT "Evens: ", EvensHow do we fill it in?
Evens ← 0 goes in the first row. No other variable has a value yet.Index becomes . A new row starts. The prompt is output, then Value is . MOD(6, 2) = 0 is true, so Evens becomes .Index becomes . The prompt again, then Value is . MOD(3, 2) is , so Evens does not change. Leave its cell empty.Index : Value , which is even, so Evens becomes .Index : Value , which is odd. No change.Evens: 2.A cell is filled only when its value changes. An empty cell means "the same as before". Each pass of a loop usually starts a new row.
Look at the last values to find the result, and at the pattern to find the purpose. Here the purpose is: "It inputs four whole numbers and outputs how many of them are even."
A trace table also finds mistakes. Suppose a programmer meant to count numbers greater than , but wrote this test.
IF Value > 6
THEN
Count ← Count + 1
ENDIFA dry run with the input shows that is not counted, though . The trace table points straight at the line to fix.
The algorithm below doubles a value until it reaches . Try the input by hand: , , , , . That is passes, and the output is 4 112.
INPUT Value
Steps ← 0
WHILE Value < 100 DO
Value ← Value * 2
Steps ← Steps + 1
ENDWHILE
OUTPUT Steps, " ", ValueThe grey blocks marked pseudocode are written in the exam style and do not run. The Python box is the same algorithm in Python, and that one runs. This Python program does the even-numbers dry run for you. It prints one row each time round the loop, so you can check your own trace table against it.
Total ← Total + Number, the old Total is used on the right.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Evens cell empty in the row where Value is ?X? X ← 2, then FOR K ← 1 TO 3, with X ← X + K inside, then NEXT K.Count, Total and OUTPUT. The algorithm outputs "Enter a price" before each input. Where do you write that message?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place in the course.