1. Code Trace
Code trace
The example below show tracing through a program one line at a time. During the
tracing process it is important to:
- keep track carefully of the values of variables
- keep track of the current line that is being executed
The history of values for each variables is given in red.
Consider the procedure mystery given below. What is returned (computed) after
the call mystery(2, 8)?
2 8
int mystery(int a, int b)
{
int c = a; 2 4 6 8 9 10 11
int d = b*c; 16 15 14 13 12 11 10
int t = 0; 0 2 6 12 20 29 39
while (c < d)
{
t = t + c;
if (c >= b)
{
c = c + 1;
}
else
{
c = c + 2;
}
d = d - 1;
}
return t;
}
Answer: mystery(2, 8) returns 39 (the final value of variable t).