Originally Posted by
salmanriaz
Hi everyone,I am in trouble in solving ackermann function..The is like this:
Please indent your code and use [code] tags.
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);
}