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?

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

Shouldn't you just pass those values from main to you function?

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

Shouldn't you just pass those values from main to you function?

I tried that but the program wouldn't stop running at all

What do lines 20-22 do? They don't seem to correspond to any part of the problem specification.

In particular, they seem to say that A(0,1) is equal to A(1,1). I see no justification for that claim.

What do lines 20-22 do? They don't seem to correspond to any part of the problem specification.

In particular, they seem to say that A(0,1) is equal to A(1,1). I see no justification for that claim.

I've just been trying numerous things to keep the program from running nonstop. I'm finna try something else and post the code

Revised Code and Output

#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 (m==0)
      return n+1;

    else if (n==0)
       return A(m-1,1);

	else
       return A(m-1, A(m,n-1));
}
A(0,0)   A(0,0)   A(0,0)   A(0,0)   A(0,0)   A(0,0)   A(0,0)   A(0,0)   Press an
y key to continue . . .

What do you expect it to do and why?

This is for my Data Structures class. This is all we were given:

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 for my Data Structures class. This is all we were given:

I understand what your assignment is.

What I am asking you to do is to explain the program that you wrote.

You say that it is producing results that you do not expect.

I am asking you what results you expect the program to produce, and why you expect the program to produce those results. An answer to that question would be something like:

"Well, the first statement sets this variable to this value, and then the next statement prints that variable, so I would expect it to print the value of the variable" and so on.

Comments
Yep

Do you realize that in your main() you never actually call your A(int int)? that's why you are getting 0,0.

I understand the program now...I have done it. Here's my results. Thanks!

#include <iostream>
using namespace std;

int A(int, int);

int main()
{
        int m, n;

		cout<<"Enter a value for m: ";
		cin>>m;

		cout<<"Enter a value for n: ";
		cin>>n;
		
		A(m,n);

        return 0;
}
int A(int m,int n)
{
	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));	
	}
}

Examples:

Enter a value for m:
0
Enter a value for n: 0
A(0,0)  Press any key to continue . . .
Enter a value for m: 1
Enter a value for n: 2
A(1,2)  A(1,1)  A(1,0)  A(0,1)  A(0,2)  A(0,3)  Press any key to continue . . .
Enter a value for m: 1
Enter a value for n: 0
A(1,0)  A(0,1)  Press any key to continue . . .

hello all,
thank you very much for your help,
is there any one to help me to solve ackermann function?
I need the proper code if possible.

This question has already been answered. Start a new discussion instead.