Question

4. (35) Suppose we have the following recursive function in C++: a. (5) What is the returned value of fib(5)? b. (10) Supp

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer a)The value returned by the function fib(5) is 5 From the definition fib(n-1)+fib(n-2) fib(5)=fib(4-1)+fib(4-2)=5 b) F

Add a comment
Know the answer?
Add Answer to:
4. (35') Suppose we have the following recursive function in C++: a. (5') What is the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For each of the following recursive methods available on the class handout, derive a worst-case recurrence...

    For each of the following recursive methods available on the class handout, derive a worst-case recurrence relation along with initial condition(s) and solve the relation to analyze the time complexity of the method. The time complexity must be given in a big-O notation. 1. digitSum(int n) - summing the digits of integer: int digitSum(int n) {                 if (n < 10)                                 return n;                 return (digitSum(n/10) + n%10); } 2. void reverseA(int l, int r) - reversing array: void...

  • Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to...

    Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to calculate the Fibonacci Function 1.) Use a recursive function called fib() to calculate the Fibonacci Function for the following values for the variable n. int n = 10; int n = 20; int n = 30; int n = 40; int n = 45; int n = 46; 2.) In addition to calculating and displaying the results, use a "timer" to see how long...

  • 3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int...

    3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int recurseFunc(int n) If n 0, return 1. If n 1, return 1 while i< n do while j <n do print("hi") j 1 end while i i 1 end while int a recurse Func(n/9); int b recurse Func (n/9) int c recurse Func (n/9) return a b c (1) Set up a runtime recurrence for the runtime T n) of this algorithm. (2) Solve...

  • PLEASE write in C Language, ( you can use array, recursive function, Pointers, Dynamic call by re...

    PLEASE write in C Language, ( you can use array, recursive function, Pointers, Dynamic call by refernce, Structure) a) 70 marks] Write a program that Sorts a given integer array via Selection Sort obtained by rotation around 3. Searches for a key point in the rotated array in O(logn) time, where the rotation Rotates the sorted array around a random point, e.g., 1 2 345->34512 is point is known in advance. ts above by first finding the unknown rotation point...

  • 2. Consider the function george (int n) computed by the following recursive C++ code. int george ...

    2. Consider the function george (int n) computed by the following recursive C++ code. int george (int n) assert (n >= 0) if (n < 2) return 1; else return 2*george ((n+1)/2)+2*george (n/2)+2 george((n-1)/2)+2*george (n/2-1); (c) Design a memoization algorithm to compute george(n) for any given n. You do not have to write C++ code. This algorithm should be much faster than the dynamic programming algorithm. What is its time complexity? 2. Consider the function george (int n) computed by...

  • Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) =...

    Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) = 13 + 23 + … + n3. (a) Set up a recurrence relation for the number of multiplications made by this algorithm. (b) Provide an initial condition for the recurrence relation you develop at the question (a). (c) Solve the recurrence relation of the question (a) and present the time complexity as described at the question number 1. Algorithm S n) Input: A positive...

  • algorithms & data structures-1 answer and explain briefly . 4. For the following recursive function, findf...

    algorithms & data structures-1 answer and explain briefly . 4. For the following recursive function, findf (5): int f(int n) if (n 0) return 0; else return n f(n - 1);

  • 2. Give the asymptotic running time of each the following functions in e notation. That is,...

    2. Give the asymptotic running time of each the following functions in e notation. That is, write down a recurrence relation for each recursive function below and solve it. Show your work def Pow(x, n): 2 if n-0: 3 return 1 end 5 e f Pow(x, [n/2]) 1 # n is even if n % 2-0: 9 return f f 10 end #nis odd 12return r*f*.f 13 end

  • c class ex in the 2pic Question 1: A. Write a recursive function Ecursive function that...

    c class ex in the 2pic Question 1: A. Write a recursive function Ecursive function that converts a string of digits into an integer. The function returns an integer that is converted from the original string 's'. int convertToint char *s); B. What's BIG-O notation of your function in part A. charx = "1234": Sở 3 : void printlnt (charts) if( *s == '() return; printf("%. Ch, ts): 7 printlnt (5+1): Ob..

  • The following algorithm (Rosen pg. 363) is a recursive version of linear search, which has access...

    The following algorithm (Rosen pg. 363) is a recursive version of linear search, which has access to a global list of distinct integers a_1, a_2,..., a_n. procedure search(i, j, x : i,j, x integers, 1 < i < j < n) if a_i = x then return i else if i = j then 4. return 0 else return search(i + 1, j, x) Prove that this algorithm correctly solves the searching problem when called with parameters i = 1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT