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.
Implement a recursive algorithm to compute the product of two integers (in java). Your algorithm can...
Implement a recursive algorithm to compute the product of two integers (in java). Your algorithm can only perform addition and subtraction; no multiplication. Analyze the running time and space complexity of your solution.
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?
Write a C program for the recursive algorithm that removes all occurrences of a specific character...
Write a C program for the recursive algorithm that removes all occurrences of a specific character from a string. (please comment the code)
note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is...
note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is allowed. The game of “Jump It” consists of a board with n positive integers in a row, except for the first column, which always contains zero. These numbers represent the cost to enter each column. Here is a sample game board where n is 6: 0 3 80 6 57 10 The object of the game is to move from the first column to...
C++ Create a recursive program what will test three recursive functions. It would have a menu...
C++ Create a recursive program what will test three recursive functions. It would have a menu like: 1. Recursive factorial 2. Towers of Hanoi 3. Recursive summation 0. Exit Enter selection: 3 Enter number: 4 The recursive summation will take an integer and sum value from 1 to the integer. Don’t enter a number if the selection is 0 for quit. Please have the functions in an implementation file and the prototypes of the functions in a header file and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT