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,...
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.
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?
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.
in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based...
in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based on the fact that xy = 2(x · (y/2)) when y is even and xy = 2(x · ⌊y/2⌋) + x when y is odd, together with the initial condition xy = 0 when y = 0.
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...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT