You are on page 1of 4

Trace Tables

A Trace Table is a table that is completed by tracing the instruction in an algorithm with given
data to arrive at solutions.
Completing a trace table is a useful tool for testing your skills in understanding how the IF,
WHILE and FOR constructs/statements operate.

Complete the trace table for the following algorithm:

A=2
B=3 A B C
C=1 2 3 1
While B < 45 Do 5 8 9
A=A+B 13 21 30
B=B+A
34 55 85
C=C+B
EndWhile

Question 1.

M=1
N=2 M N P
P=1 1 2 1
WHILE M < 30 Do 5 2 3
N=M*2 13 10 11
P=N+1
29 26 27
M=P+2
61 58 59
End WHILE

Assignment
N J S
READ N 6 0 12
If J < 5
6 1 14
S = (N + J) * 2
6 2 16
ELSE
6 3 18
S = (N – J) * 2
ENDIF 6 4 20
ENDFOR 6 5 2
How to trace an algorithm:

- Start tracing from the top of the algorithm and write down the appropriate value in
the first vacant cell for the variable which is currently being assigned a value.

- Remember when calculating that value, that a variable can only store one item of
data at a given time.

- If the values of variables are to be printed, write these values on your answer sheet
as soon as the values are obtained.

- In tracing the values in an algorithm you must observe all conditions and loops.

What is printed by the following algorithms?

A=5 D=5
B=6 E=6
C=4 F=4
D=A A=4
E=B B=5
F=C C=6
A=F 4,5,6
B=D
C=E
Print A,B,C

T=3
S = (4 + 2) * 3
P=2
IF T > 3 OR P > 3 THEN
18
N=5
ELSE
N=4
ENDIF
S = (N + P)*T X=0
PRINT S X=0+1=1
X=1+1=2
X=2+1=3
X=0 X=3+1=4
WHILE X < 6 DO X=4+1=5
X=X+1 1
PRINT X 2
ENDWHILE 3
4
5
J=1
J=2
TOTAL = 0 J=3
FOR J = 1 TO 4 DO J=4
IF J < 3 THEN
TOTAL = TOTAL + 2 TOTAL = 2
ELSE TOTAL = 4
TOTAL = TOTAL + 3 TOTAL = 7
ENDIF TOTAL = 10
PRINT TOTAL
ENDFOR 2
4
7
10

TOT = 0
READ S
S T Q TOT
FOR P = 1 TO S DO
READ T 0
IF T > 5 THEN
Q=P*T 4 4
ELSE
IF T <= 5 AND T > 3 THEN 4 6
Q=P*T+2
4 3
ELSE
Q=P*4 4 5
ENDIF
ENDIF
TOT = TOT + T
ENDFOR
This part is based on the algorithm given below:

Set A to 3
Set B to 5
Set SUM to 1
While A <= 50 do
A=A+B
B=B+A
SUM = SUM + B
Endwhile
Print SUM
Stop

Using the algorithm above:

1. Copy and complete the trace table which follows

A B SUM
3 5 1
8 13 14
21 34 48
55 89 137

2. What is printed by the algorithm 137

You might also like