Question

4. For the following recursive function, findf (5): int f(int n) if (n 0) return 0; else return n f(n - 1);

algorithms & data structures-1

answer and explain briefly .

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

Answers:

5 o) elu e L4) elke 0; (30) 어efunn o; else ent f2) eue int f(o) e lbe un 1 2018-3-26 18:25

explanation:

first of all the f() is called from main() in function integer 5 is passed as an argument .
then f(5) is added to the result of f(5-1).
the next function call from f() to f() 4 is passed which is added to the result of f(3).
this process continues until n is equal to 0
when n is equal to 0 then it return to f(1) and f(1) return to the value to the f(2) and till f(5) we have to backtrack from it.

the codeing part of this question:

#include <stdio.h>

int fun(int n)
{
   if (n == 0)
   return 0;
   else
   return n+fun(n-1);
}


int main()
{
int n=5;
printf("%d ", fun(n));
return 0;
}

output:

15

Add a comment
Know the answer?
Add Answer to:
algorithms & data structures-1 answer and explain briefly . 4. For the following recursive function, findf...
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
  • 1. Determine what the following function calls return for recursive function func below.     (4 pts.)...

    1. Determine what the following function calls return for recursive function func below.     (4 pts.)               public static int func(int n)      {         if(n == 1)            return 2;         else            return 2 + func(n-1);                      (a) func(1) = ________        (b) func(4) = ________ 2. Does func above perform top down or bottom up computation? ____________ (2 pts.)

  • 4. (35') Suppose we have the following recursive function in C++: a. (5') What is the...

    4. (35') Suppose we have the following recursive function in C++: a. (5') What is the returned value of fib(5)? b. (10') Suppose the time cost for fib(n) is T(n), write down the recurrence relation for T(n). C. (20') Rewrite this function in C++ which gives exactly the same output and make it of lower complexity. Analyze the time cost for your function in big-oh notation. int fib (int n) { if (n <= 0) return 0; if( n ==...

  • This is for C in Linux: Problem 1: Given the following recursive function: void recursiveFunction( int...

    This is for C in Linux: Problem 1: Given the following recursive function: void recursiveFunction( int m ) printf("%d", m); if( m <= 0 ) return; if( n + 2 == 0 ) recursiveFunction( m - 1); else recursiveFunction( m - 2); What is the output when the following functions are called with these parameters? recursiveFunction( 5 ); recursiveFunction( 10 ); recursiveFunction( 0);

  • The code for a recursive function 'mystery' appears below. Assume we passed the following array as...

    The code for a recursive function 'mystery' appears below. Assume we passed the following array as x[]: int x[] = { 10, 20, 25, 25 }; How would we call mystery() so the return value is 1? int mystery(const int x[], int n, int mysteryValue) { int count = 0; if (n <= 0) return 0; else { if (x[n - 1] == mysteryValue) count = 1; return mystery(x, n - 1, mysteryValue) + count; } } 1mystery(x, 4, 0)...

  • 3a. Runtime Analysis: For each of the following recursive algorithms, state the recurrence equation that is...

    3a. Runtime Analysis: For each of the following recursive algorithms, state the recurrence equation that is the runtime of the algorithm. Briefly describe how you determined each term. You do not need to provide the runtime bound – just the equation. T(n) = Product(array, p) // n is size of array if (p>n) return 1 if (array[p] != 0) return array[p] * Product(array, p+1) else return Product(array, p+1) Describe terms: T(n) = Describe terms: // modified merge sort mod Merge(...

  • *Program is in C* Write a recursive function to compute a^b for integers a and b....

    *Program is in C* Write a recursive function to compute a^b for integers a and b. For the recursive use the following equality a^b = a^b - 1 * a and a^0 = 1. What does the following recursive function do? int mystery(int a, int b) {if (b == 1) return (a); else return (a + mystery(a, b - 1));}

  • What is the output of the following java function when called as f(1,25,25)? Int f(int a,...

    What is the output of the following java function when called as f(1,25,25)? Int f(int a, int b, int n){ int mid =(a+b)/2; if ((mid*mid <=n) && (n< (mid+1)*(mid+1)) return mid; else If (mid*mid>n) return f(a,mid-1,n); else return f(mid+1,b,n); } B.) Is the following function tail-recursive? Acker(m,n){ if(m=0) n+1 else if(n=0) Acker(m-1,1) else Acker(m-1,Acker(m,n-1)) }

  • C++ Assignment 4 - For example 2, write a non recursive version of this function... using...

    C++ Assignment 4 - For example 2, write a non recursive version of this function... using a regular loop. Let's consider writing a function to find the factorial of an integer, N!. For example 7! equals 7*6*5*4*3*2*1. int myFactorial( int integer) if( integer == 1) return 1; else return (integer * (myFactorial (integer-1))); // action performed on call - pass into function "integer - 1" // action performed on return *

  • Q5. [5 marks] Consider the following recursive function: int Test (int number) //Line 1 //Line 2...

    Q5. [5 marks] Consider the following recursive function: int Test (int number) //Line 1 //Line 2 if (number == 0) //Line 3 return number; //Line 4 else //Line 5 return (number + Test (number - 1)); //Line 6 //Line 7 a. Identify the base case. b. Identify the general case. c. If Test (0) is a valid call, what is its value? If not, explain why. d. If Test (5) is a valid call, what is its value? If not,...

  • 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...

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