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)
This is my code:
#include <iostream>
using namespace std;
int A(int, int);
int main()
{
int m=NULL, n=0, count=0;
while(count<=7)
{
cout<<A(m,n);
count++;
};
return 0;
}
int A(int m,int n)
{ if(n==1 && m==0)
{
m+=1;
}
if (m==0)
{
cout<<"A("<<m<<","<<n<<")";
return n+1;
}
else if (n==0)
{
cout<<"A("<<m<<","<<n<<")";
return A(m-1,1);
}
else
{
cout<<"A("<<m<<","<<n<<")";
return A(m-1, A(m,n-1));
}
}
My output is:
A(0,0)1A(0,0)1A(0,0)1A(0,0)1A(0,0)1A(0,0)1A(0,0)1A(0,0)1Press any key to continu
e . . .
Wat am i doing wrong?