Question

Consider the following recursive definition of a factorial function. int factorial ( int n) {   ...

Consider the following recursive definition of a factorial function.

int factorial ( int n)

{

   if ( n == 0 || n ==1 )

   return 1;

      else

   return n * factorial (n-1);

   }

Suppose the function factorial (4) is invoke. Trace through the function call, explicitly show how the factorial function is repeatedly called and what is the value of n in each call. Also show the value returned by each call.

Give an iterative implementation of the factorial function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
factorial (4) 
= 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * 2 * factorial(1)
= 4 * 3 * 2 * 1
= 24

====================================

Iterative version:
int factorial ( int n)
{
    int result = 1;
    for(int i = 1;i<=n;i++){
        result = result * i;
    }
    return result;
}
Add a comment
Know the answer?
Add Answer to:
Consider the following recursive definition of a factorial function. int factorial ( int n) {   ...
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
  • Recursive definition for factorial: a0 = 1, an = n * an-1 procedure factorial(n: nonnegative integer)...

    Recursive definition for factorial: a0 = 1, an = n * an-1 procedure factorial(n: nonnegative integer) if n = 0 then return 1 else return n * factorial( n - 1 ) Trace the execution of the factorial algorithm described above for input 7. Track the number of times factorial is invoked (with the first invocation with input 7 as invocation 0) and the value returned by each invocation.

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

  • The following recursive method factRecursive computes the factorial of positive integer n. Demonstrate that this method...

    The following recursive method factRecursive computes the factorial of positive integer n. Demonstrate that this method is recursive. public static int factRecursive(int n) { int result = 0; if (n == 0) { result = 1; } else { result = n * factRecursive(n - 1); } return result; }

  • LC3 stack (factorial) I need help in writing factorial in Lc3 language by using stack.. ; Begin ...

    LC3 stack (factorial) I need help in writing factorial in Lc3 language by using stack.. ; Begin reserved section: do not change ANYTHING in reserved section! .ORIG x3000 BR Main ; Parameter and result Param .FILL x0004 Result .BLKW 1 ; Constants Stack .FILL x4000 One .FILL #1 MinusOne .FILL #-1 ; End reserved section: do not change ANYTHING in reserved section! ;------------------------------------------------------------------------------- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; int Factorial(int N) ; Returns N! (must be a recursive function) ; Factorial ;__________________...

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

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

  • int binSearch(int arr[], int lo, int hi, int x) { int q = (low + high)...

    int binSearch(int arr[], int lo, int hi, int x) { int q = (low + high) / 2; if(arr[q] <= x && arr[q + 1] > x){ return q + 1; } else if(arr[q] == x){ return binSearch(arr, q + 1, high, x); } else if(arr[q] > x){ return binSearch(arr, low, q - 1, x); } else if(arr[q] < x){ return binSearch(arr, q + 1, high, x); } } Based on the above function, write an iterative algorithm (based on...

  • The language has to be in C program 1. (a) rite a function, int factorial (int...

    The language has to be in C program 1. (a) rite a function, int factorial (int n), which returns n (the factorial of n, i e. 1 x2 x3 x...xn.) (b) Using int factorial (int n) above, write a program to compute 2. We want to find one of the roots of a cubic equation given by which is between 0 and 1 by an iterative method. Modify the equation above to 1 x Start with an appropriate initial value...

  • Recursive Tracing. For each call to the following method, indicate what value is returned: public static...

    Recursive Tracing. For each call to the following method, indicate what value is returned: public static int mystery(int n) { if (n < 0) { return -mystery(-n); } else if (n == 0) { return 0; } else { return mystery(n / 10) * 10 + 9 - (n % 10); } Call Value Returned mystery(0) mystery(5) mystery(13) mystery(297) mystery(-3456) } Can any one help me with it?

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