Trace Table Notes and Activity
Trace Table Notes and Activity
It is best to check the algorithm for correctness before it is converted to code. A trace table is a
method used to test an algorithm for correctness
Example 1
Test the correctness of the following pseudocode
Sum = 0
Read number
While number <> 0 do
Sum = Sum+ number
Read number
Endwhile
Print “Sum of numbers is,” Sum
Sum Number
0 12
12 23
35 34
69 0
Example 2
Test the logics of the following pseudocode for correctness using a trace table. Use the
following as your test data, -12, 23, 30, 0, -120, 50, 0, 0, 15, 999
Start
Ncount=0
Zcount=0
Possum=0
Read num
While num <> 999 do
If
Num<0 then
Ncount ← Ncount + 1
Endif
If num=0 then
zcount ←zcount+1
Endif
If num>0 then
possum←possum+num
endif
read num
Endwhile
Print “number of negative numbers is”, Ncount
Print “Number of zero’s is”, Zcount
Print “Sum of positive number is”, Possum
End
0 0 0 -12
1 1 23 23
2 2 53 30
3 103 0
118 -120
50
15
999
Example 3
Complete the trace table for the following algorithm given that the number 4 is the input value
for x
Read x
For M = 1 to x do
Y =x–M
Z =5*Y–M
Endfor
Print Z
X M Y Z
4 1 3 14
4 2 2 8
4 3 1 2
4 4 0 -4
Class Activities
Do a trace table for the following SIX algorithm
Biggest =0
For Counter 1 to 10 do
Read num
If num > Biggest
Biggest ← num
Endif
Print Biggest
Complete the trace table below for the following algorithm if x = 5, y = 10, Z = 15. What will be
printed by the following algorithm?
Read X
Read Y
Read Z
Set K ← X
Set Y ← Z
Set Z ← K
Set Y ← Z
Write X, Y, Z, K
Sum = 0
N = 10
While N < 40 do
Sum ← Sum + N
Print N, Sum
N = N+5
Endwhile
Count = 1
X=2
While Count < 25 do
X=X+2
Count = Count + 5
Endwhile
X=5
Read 10
Sum = 45
While sum < 75 do
Sum = Sum + K
K=K+X
Endwhile
X=4
Read X
For Q = 1 to X do
Y =4–Q
Z = 10 + Y – Q
endfor