Question

In: Computer Science

C++ Ackermann’s function is a recursive mathematical algorithm that can be used to test how well...

C++

Ackermann’s function is a recursive mathematical algorithm that can be used to test how well a computer performs recursion. Write a function A(m, n) that solves Ackermann’s function. Use the following logic in your function:

If m = 0 then      return n + 1

If n = 0 then       return A(m−1, 1)

Otherwise,          return A(m–1, A(m, n–1))

Test your function in a driver program that displays the following values:

A(0, 0)   A(0, 1)   A(1, 1)    A(1, 2)    A(1, 3)     A(2, 2)    A(3, 2)

Solutions

Expert Solution

#include<iostream>
using namespace std;

int ackermann(int m, int n){
   if(m == 0){
      return n+1;
   }
   else if(m>0 && n==0){
      return ackermann(m-1,1);
   }
   else{
      return ackermann(m-1,ackermann(m,n-1));
   }
}

int main(){
   cout<<"ackermann(0,0) = "<<ackermann(0,0)<<endl;
   cout<<"ackermann(0,1) = "<<ackermann(0,1)<<endl;
   cout<<"ackermann(1,1) = "<<ackermann(1,1)<<endl;
   cout<<"ackermann(1,2) = "<<ackermann(1,2)<<endl;
   cout<<"ackermann(1,3) = "<<ackermann(1,3)<<endl;
   cout<<"ackermann(2,2) = "<<ackermann(2,2)<<endl;
   cout<<"ackermann(3,2) = "<<ackermann(3,2)<<endl;
   return 0;  
}


Related Solutions

Ackermann’s function is a recursive mathematical algorithm that can be used to test how well a...
Ackermann’s function is a recursive mathematical algorithm that can be used to test how well a computer performs recursion. Write a method ackermann(m, n), which solves Ackermann’s function. Use the following logic in your method: If m = 0, then return n + 1 If n = 0, then return ackermann(m-1, 1) Otherwise, return ackermann(m -1, ackermann(m, n - 1)) Test your method in a java program that displays the return values of the following method calls: ackermann(0, 0), ackermann(0,...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays of integers. Neither list contains duplicates, and the resulting list should not contain duplicates either. Hint: You may want to call a helper function from merge. PROGRAM: C
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;...
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;   f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2 b) Solve f(n) as a function of n using the methodology used in class for Homogenous Equations. Must solve for the constants as well as the initial conditions are given.
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a...
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a specific character from a string b. Write the pseudocode for the program.
In the case of c++, how would I store data drom a recursive function into a...
In the case of c++, how would I store data drom a recursive function into a 2D array?
For C++ Function 1: Write a recursive function to perform a sequential search on a set...
For C++ Function 1: Write a recursive function to perform a sequential search on a set of integers The function will require an array parameter and the number to look for. These are the minimal parameter requirements The function should take an array of any size Function 2: Write a recursive function that will convert an integer (base 10) to binary The function should only have an integer parameter Have the function write the binary number to the console You...
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main...
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main is already set. Hint: It might be helpful to modify the function so that it uses a separate recursive function to perform whatever processing is needed. //////////////////////////////////////////////////////////////header.h////////////////////////////////////////////////////////////////////////////////////////////// #ifndef HEADER_H_ #define HEADER_H_ #include <iostream> using namespace std; template <class T> class LL { private:    struct LLnode    {        LLnode* fwdPtr;        T theData;    };    LLnode* head; public:    LL();...
Explain the benefits a recursive algorithm can provide. Discuss the negative aspects of using recursion.
Explain the benefits a recursive algorithm can provide. Discuss the negative aspects of using recursion.
There are two restrictions on the type of grammars that can be used with a recursive...
There are two restrictions on the type of grammars that can be used with a recursive descent parser. The first is that the grammar cannot have any left recursive productions. Give an example of a left recursive production and explain why such productions would be a problem. The second restriction is that the grammar must not require more than one token look ahead. Give an example of a production that does not have this property. Explain why this restriction is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT