1. ## stack library??

Hi everyone,I am in trouble in solving ackermann function..The is like this:

int Acker(int m, int n)
{
if (m == 0)
return (n + 1);
else if (n == 0)
return (Acker(m - 1,1));
else
return (Acker(m - 1,Acker(m,n - 1)));
}

In this,i have to find out those numbers,on which this function fails to produce result.Means that if i have taken two integer numbers,then if the result is much larger that integer can not hold that result.For example if integer range is 32767 than i have to find that number on which it fails.
Problem is that when I call the function with value(4,2),then program hang out in continous recursion..Can anyone have idea to how to catch the stack overflow error when the program starts to hang...is there any library related to stack.???

2. Stack overflow would usually cause the program to crash (segmentation fault) instead of hang. But of course, this is technically undefined behaviour.

For memory debugging, if you are on Linux, there is valgrind.

3. Originally Posted by salmanriaz
Hi everyone,I am in trouble in solving ackermann function..The is like this:
Problem is that when I call the function with value(4,2),then program hang out in continous recursion..Can anyone have idea to how to catch the stack overflow error when the program starts to hang...is there any library related to stack.???
reformattign your code to make it more legible reveals this -
Code:
```int Acker(int m, int n){
if (m == 0) return (n + 1);

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

else return (Acker(m - 1,Acker(m,n - 1)));
}```
so you are not properly implementing the else statements.

enclose the returns in braces {} and it should solve your problem., although you should also understand that -
Originally Posted by Wikipedia
In computability theory, the Ackermann function or Ackermann–Péter function is a simple example of a computable function that is not primitive recursive.

Its value grows rapidly, even for small inputs. For example A(4,2) is an integer of 19,729 decimal digits
Code:
```int Acker(int m, int n){
printf("Acker(%d , %d);\n" , m , n);

if (m == 0)				return (n + 1);
if (m > 0 && n == 0)	return (Acker(m - 1,1));
if (m > 0 && n > 0)		return (Acker(m - 1,Acker(m,n - 1)));

printf("Error, halting!!!\n");
while(1) Sleep(100);
}```