Ackermann's Function in C++

 
0
 

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?

Featured Replies in this Discussion

  • ... 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,…
    Post Contains: Quotes
  • If you have the same problem, it can be solved in the same way.
 
0
 

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?

 
0
 

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

 
0
 

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.

 
0
 

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

 
0
 

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 . . .
 
0
 

What do you expect it to do and why?

 
0
 

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)

 
0
 

You need to call your function for those values... Passing (0,0) to A gives ____ ?

 
1
 

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
 
0
 

Do you realize that in your main() you never actually call your A(int int)? that's why you are getting 0,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 . . .
Question Answered as of 4 Years Ago by arkoenig, iamthwee, jonsca and 1 other
 
-1
 

its give me 0,0, i have the same problem how can it be solved ?

 
2
 

If you have the same problem, it can be solved in the same way.

 
0
 

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.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article