stack library??

This is a discussion on stack library?? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone,I am in trouble in solving ackermann function..The is like this: int Acker(int m, int n) { if (m ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    33

    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. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    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. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by salmanriaz View Post
    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 -
    Quote 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);
    	}
    Last edited by abachler; 09-27-2009 at 10:11 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  2. Stack
    By planet_abhi in forum C Programming
    Replies: 2
    Last Post: 04-12-2003, 04:22 AM
  3. inputting line of text vs. integers in STACK
    By sballew in forum C Programming
    Replies: 17
    Last Post: 11-27-2001, 10:23 PM
  4. stack make file problem
    By puckett_m in forum C Programming
    Replies: 2
    Last Post: 11-22-2001, 10:51 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21