177k views
3 votes
Look at rem() again:

a.rem(a,d) = a if a < dotherwise
b.rem(a,d) = rem(a-d, d)
Assume the definition has been implemented correctly. How many activations will there be on the activation chain if main() calls rem( 20,5 )?

1 Answer

6 votes

Final answer:

The function rem(20, 5) will have 5 activations on the activation chain as it reduces 20 by intervals of 5 until reaching 0.

Step-by-step explanation:

The activation chain refers to the number of times a recursive function calls itself before reaching a base case. Here we have the recursive function rem() which calculates the remainder when a is divided by d. The main question is concerning the number of activations for rem(20, 5).

Since d is 5, and 20 is divisible by 5, the recursive calls will continue until a is less than d. We start with a = 20 and reduce a by d (which is 5) in each call. This results in the following activations:

  1. rem(20, 5) - which calls
  2. rem(15, 5) - which calls
  3. rem(10, 5) - which calls
  4. rem(5, 5) - which calls
  5. rem(0, 5) - which hits the base case a < d

Thus, there will be 5 activations on the activation chain for this example.

User Folayan
by
7.9k points