Docsity

Tracing Program with Boxes and Arrows - Guidelines | CS 110, Exams of Computer Science

Toggle sidebar
bg1
CS 110: Introduction to Computer Science Spring 2008
Tracing a Program with Boxes and Arrows
Guidelines
1. Start execution in the 'main' program.
2. When a new variable is referred to create a memory cell (name of
variable with a line under it) for it and place its value in it. Put the
memory cell to the right of the main code.
3. When variables change, cross out the old value and put the new
value underneath.
4. When a function is called:
a. Create a box for all parameters and local variables of the
function.
b. If a parameter is a scalar, move the value of each actual value
into the box for the corresponding formal parameter.
c. If a parameter is a list, draw an arrow from the formal
parameter box to the box for the corresponding actual
parameter.
d. Begin execution of the function statements. If a list parameter
is modified, follow the arrow to actually change the values.
e. When reaching a return statement in the function, move the
return value into the variable in the caller that catches the
return value.
Example (right box shows final state of execution)
def totalList (list):
total= 0
i=0
while i<len(list):
total=total+list[i]
i=i+1
return total
# main
scores=[2,4]
total=totalList(scores)
print total
list total i
0 0
2 1
6 2
scores total
[2,4] 6
pf2

Partial preview of the text

Download Tracing Program with Boxes and Arrows - Guidelines | CS 110 and more Exams Computer Science in PDF only on Docsity!

CS 110: Introduction to Computer Science Spring 2008

Tracing a Program with Boxes and Arrows

Guidelines

  1. Start execution in the 'main' program.
  2. When a new variable is referred to create a memory cell (name of variable with a line under it) for it and place its value in it. Put the memory cell to the right of the main code.
  3. When variables change, cross out the old value and put the new value underneath.
  4. When a function is called: a. Create a box for all parameters and local variables of the function. b. If a parameter is a scalar, move the value of each actual value into the box for the corresponding formal parameter. c. If a parameter is a list, draw an arrow from the formal parameter box to the box for the corresponding actual parameter. d. Begin execution of the function statements. If a list parameter is modified, follow the arrow to actually change the values. e. When reaching a return statement in the function, move the return value into the variable in the caller that catches the return value.

Example (right box shows final state of execution)

def totalList (list): total= 0 i= while i CS 110: Introduction to Computer Science Spring 2008

Worksheet

  1. Circle and label the following elements in the code below: formal parameters, actual parameters, object-oriented function calls, function signatures.
  2. Trace the code below. def initList1(n): i= list=[] while i