Question

. Determine what is calculated for n = 5: unsigned int F(unsigned int n) { if(n...

. Determine what is calculated for n = 5:

unsigned int F(unsigned int n)

{

if(n == 0)

return 1;

return n ∗ F(n − 1);

}

Write an iterative version of the function in this problem

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Answer :
F(5) = 5 * 4 * 3 * 2 * 1 * 1 = 120

iterative version of the function is

unsigned int F(unsigned int n){
    unsigned int result = 1;
    while(n>0){
        result *= n;
        n--;
    }
    return result;
}
Add a comment
Know the answer?
Add Answer to:
. Determine what is calculated for n = 5: unsigned int F(unsigned int n) { if(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
  • We have omitted the definitions of constants M and N unsigned int arith(unsigned int x, unsigned...

    We have omitted the definitions of constants M and N unsigned int arith(unsigned int x, unsigned y) { unsigned int result = 0; result = x * M + y/N; return result; } After compiler optimization for multiplication and division: unsigned int optarith(unsigned int x, unsigned int y) { unsigned int t = x; x += t; x <<= 9; y >>= 6; return x + y; } M=? and N=?

  • Given the following recursive function: unsigned int mymod( unsigned int a, unsigned int b ) {...

    Given the following recursive function: unsigned int mymod( unsigned int a, unsigned int b ) { if( a < b ) return a; return mymod( b - a, b ); } the function call mymod(5, 2) will Select one: a. execute infinitely and terminates with an error. b. execute until some if-condition matches. c. not compile, it will generate a compilation error. d. return the value: 1

  • What does this function return if it is called with n = 4? int f(int n)...

    What does this function return if it is called with n = 4? int f(int n) { if (n == 0) return 1; else return f(n-1) + 1; } Can you show the steps as well please?

  • #include <stdio.h> int isValidCC(unsigned long long int CCNumber); int main() { unsigned long long int CCNumbers[]...

    #include <stdio.h> int isValidCC(unsigned long long int CCNumber); int main() { unsigned long long int CCNumbers[] = { 4388576018410707ULL, // valid 4388576018402626ULL, // invalid 7388576018402686ULL, // invalid 438857601810707ULL, // invalid 4012888888881881ULL // valid }; for (int i = 0; i < sizeof(CCNumbers) / sizeof(CCNumbers[0]); i++) { if (isValidCC(CCNumbers[i])) { printf("%llu is a valid Visa number.\n", CCNumbers[i]); } else { printf("%llu is not a valid Visa number.\n", CCNumbers[i]); } } } int isValidCC(unsigned long long int CCNumber) { // TO DO...

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

  • Q5 (25pts) Consider the code: int foo(int N){ if (N <= 3) return 2; int res1 = 3*foo(N-4); int...

    Q5 (25pts) Consider the code: int foo(int N){ if (N <= 3) return 2; int res1 = 3*foo(N-4); int res2 = foo(N-2); return res1-res2; } a) (6 points) Write the recurrence formula for the time complexity of this function (including the base cases) for N>=0. You do NOT need to solve it. b) (5 points) Draw the tree that shows the function calls performed in order to compute foo(8) (the root will be foo(8) and it will have a child...

  • 5.43 (10 pts) What does the following program do? #include <stdio.h> 3 unsigned int mystery Cuns...

    5.43 (10 pts) What does the following program do? #include <stdio.h> 3 unsigned int mystery Cuns igned int a, unsigned int b): // function prototype 5 int main(void) printf("%s". "Enter two positive integers: unsigned int x: I/ first integer unsigned int y: // second integer scanf("Su%u". &x, &y); "); 12 13 14 15 II Parameter b must be a positive integer 16 to prevent infinite recursion 7 unsigned int mystery Cuns igned int a, unsigned int b) 18 printf("The result...

  • 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)) }

  • (5 pts) What is the ouput of the following function F, for the call int i...

    (5 pts) What is the ouput of the following function F, for the call int i = F(3)? int F(int n) { int result; if (n > 20) return 1; else { result = F(2*n) * 2; cout << result << " "; return result; } }

  • In C, thanks. #include <stdio.h> void set-flag (unsigned int* flag-holder , int flag-position); void unset-flag (unsigned...

    In C, thanks. #include <stdio.h> void set-flag (unsigned int* flag-holder , int flag-position); void unset-flag (unsigned int * flag-holder, int flag-position); int check-flag (unsigned int flag-holder , int flag-position); void display -32_flags (unsigned int flag-holder); int main(int argc, char* argv (1) unsigned int flag -holder = 0; set-flag (& flag-holder, 3); set-flag (& flag-holder, 16); set-flag (& flag-holder, 31); display-32-flags (flag-holder); unset-flag(& flag-holder , 31); unset-flag (& flag-holder, 3); set-flag (& flag-holder , 9); display-32-flags (flag-holder ); return 0; Write...

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