Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the following three recursive functions in C++ a) The factorial functi

ID: 3682424 • Letter: I

Question

Implement the following three recursive functions in C++

a) The factorial function that accepts a nonnegative integer as the argument n and returns the factorial value for the argument

b) The fibonacci function that accepts a nonnegative integer as the argument n and returns the n-th fibonacci term

c) The Ackermann function that accepts two nonnegative integers as the arguments (m, n) and returns a positive integer

Code Requirements

1) You must output the value(s) of the current argument(s) whenever a function is invoked (one way of doing it is by putting an output statement in the very beginning of the function body, that prints out the argument value(s) before you do anything with the argument value(s)).

2) You must have a main function that tests (invokes) your functions and you should have one program that consists of the main function and 3 recursive functions.

Use the following set of arguments to test your functions

The Factorial function: Invoke it with values 0,1,2,3,4,5

The Fibonacci function: Invoke it with values 0,1,2,3,4,5

The Ackermann function: Invoke it with the following value pairs: (0,0), (1,1), (2,2), (2,3)

3) Include a README file that explains how to compile and run your program. Also include in the README file how the expected output would look like when your program is run.

Explanation / Answer

Factorial

Fibonacci

#include<stdio.h>

int fibonacci(int n)

{

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

    {

        return(n);

    }

    else

    {

        return(fibonacci(n-1)+fibonacci(n-2));

    }

}

int main()

{

    int n,i=0;

    printf("Input the number of terms for Fibonacci Series:");

    scanf("%d",&n);

    printf("nFibonnaci Series is as followsn");

    while(i<n)

    {

        printf("%d ",fibonacci(i));

        i++;

    }

    return 0;

}

Ackermann function


int Ackermann(int x, int y)
{
// Base or Termination Condition
if (0 == x)
{
return y + 1;
}
// Error Handling condition
if (x <> 0 && 0 == y)
{
return Ackermann(x-1, 1);
}
// Recursive call by Nested method
else
{
return Ackermann(x-1, Ackermann(x, y-1));
}
}


Compile Time Version Code
Code:


// Base Or Termination condition
template
struct Ackermann<0,>
{
enum { val = y + 1 };
};

// Recursive Call by Linear Method
template
struct Ackermann
{
enum
{
val = Ackermann::val
};
};
// Recursive Call by Nested Method
template
struct Ackermann
{
Enum
{
val = Ackermann ::val>::val
};
};