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?
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...
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.
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that...
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that computes the sum of the digits in an integer. Use the following function header: int sumDigits(int n) For example, sumDigits(234) returns 2 + 3 + 4 = 9. Write a test program that prompts the user to enter an integer and displays its sum.
In c++ Write a recursive driver function that will replace each of the odd values in...
In c++ Write a recursive driver function that will replace each of the odd values in a stack with the cube of the value.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT