Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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). See RecFact.java posted on Isidore. You are also provided with the code for a method computing the Fibonacci numbers.

Modify the Fibonacci number computing program to add the self-tracing feature.

Modify the following code

 final static Scanner cin = new Scanner(System.in); public static void main(String[] args) { out.println("CPS 151 In-class assignment 3 by ___ your name here ___ "); out.print("Enter n: "); int n = cin.nextInt(); long f = fib(n); out.println(" fib(" + n + ") = " + f); out.println(" CPS 151 In-class assignment 3 complete"); } // end main // standard fibonacci number computation using recursion static long fib(int n) { if (n < 0) terminate("Negative argument value not allowed, program ended"); if (n <= 1) return n; else return fib(n - 1) + fib (n - 2); } // end fib private static void indent(int level) { for(int Lcv = 1; Lcv <= level; Lcv++) out.print(" "); } // end indent private static void terminate(String errorMessage) { out.println(errorMessage); System.exit(1); } // end terminate

to get the following output

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books

Students also viewed these Databases questions

Question

A highly placed source explained the top secret negotiations.

Answered: 1 week ago

Question

Theyre selling a well designed machine.

Answered: 1 week ago