Trace Tables
Trace tables help you check (trace) the program line by line, and see what is happening to each variable as the program is executed.
Take this simple program:
x=5
t=1
d=t+x
x=t+d
t=t*x
print x,t,d
It would be complicated to figure out the values of each variable at the end. A trace table makes it simple:
Put the names of the variables at the top of columns in a table:
x t d
As the first line is executed we get 5 0 0 (t and d are still 0)
On the next line it becomes 5 1 0
On the third line d=t+x 5 1 6
On the fourth line x=t+d 7 1 6 (t,d don't change)
On the fifth t=t*x 7 7 6
So the final values of the variables are 7, 7 and 6.
Since we have a "print" statement, we would add it to the column headings. The actual table could be rewritten like this, combining lines 1,2 and 3 into 1 line on the table :
x t d print
5 1 6 -
7 7 6 7,7,6
Following the work in class, try this example for homework (due 1 December 2005 at 8.50am)
g=5
h=2
for d=1 to 5
h=h*h
g=h+g
next d
print d,g,h
A prep session for this will be offered on Thursday lunchtime 1-1.20pm in Room 21, and again Monday 3.10-3.50pm (to include revision of the SBA)
0 Comments:
Post a Comment
<< Home