computer science homework

In class we looked at having a recursive method trace itself to show the calls being made and the values being returned from each call (with appropriate indentation). Your task is to add this capability to a recursive function calculating Fibonacci numbers.

 

 

We can add a parameter to a recursive function to tell it what its depth of recursion is. The top level call is at depth (or level) 0. When a function executing at level k calls itself, that function call is at level k + 1. The trace messages are written to the standard output with an indentation proportional to the call level.

 

 

I have given an example adding the self-tracing feature to a function computing n! (n factorial). You are also provided with the code for a method computing the Fibonacci numbers.

 

Your task is to modify the Fibonacci number computing program to add the self-tracing feature. See how it was done for the factorial function, and do the same for the Fibonacci function.

 

Note: Each function entry message should show the value of the parameter n. Each function exit message should show the value of the parameter n, and the value of the function being returned, again suitably indented.

Note: Both recursive calls within the code for function fib (with arguments n−1 and n−2) are made at the same recursion level (recursion depth) and it is 1 more than the level at function entry. 

"Order a similar paper and get 100% plagiarism free, professional written paper now!"

Order Now