Play Computer

COMP1406 - Winter 2014 - Tutorial 9
Note: The intention of the play computer portion of the tutorial is for you trace
out, by hand, what the given code is doing. That is, you play the role of the
computer. The intention is not for you to implement anything.
1
Play Computer
è Consider the following functions:
repeatHelper(int counter, int accumulator):
1:
if counter is zero
2:
return accumulator
3:
else
4:
return repeat(counter - 1, accumulator + 2)
repeat(int n):
1:
return repeatHelper(n, 0)
Trace the code when repeat(10) is called. What is the output of this call?
1
è Consider the following functions.
Tic(int n):
1: if n <= 2
2:
return
3: else if n
4:
return
5: else
6:
return
1
is even
3*Tac(n/2)
Tac(int n):
1: if n <= 2
2:
return
3: else if n
4:
return
5: else
6:
return
3
is even
2*Tic(n/2) + 2
2*Tic( n/4 )
// usual integer division
3*Tac( n/2 ) + 1
// usual integer division
What is the output when Tic(56) and Tic(57) are called? Trace the code (keep track of
which function is being called and what its input value i).
2