Thread: Ackermann Function w/ a twist.

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    98

    Ackermann Function w/ a twist.

    function header:

    void acker (int m, int n, int &result)

    There's no return type so I can't return things for each recursive call... Instead I need to use result somehow to hold the final value. This is the first time I've ever heard of the Ackermann function and I'm trying to wrap my head around how it works so I can figure out how to use result to get the final value.

    [edit] this is especially problematic if you consider that in the case that:

    for A(m,n)
    if m>0 AND n>0 then the call should look like:
    A(m-1, A(m,n-1))
    but if the return type is void I can't pass a void to a parameter that takes an int obviously...

    Right now, the call #s grow so large I'm not sure how to do it.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if your problem is just return type write
    Code:
    int MyAcker(int m, int n)
    {
       int result =0;
       acker ( m,  n,  &result);
       return result;
    }
    and write your recursive code using this function instead
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by vart View Post
    if your problem is just return type write
    Code:
    int MyAcker(int m, int n)
    {
       int result =0;
       acker ( m,  n,  &result);
       return result;
    }
    and write your recursive code using this function instead
    I need to write it intentionally without the return type.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try writing it with an int return type first. Changing it to use the reference parameter as an out parameter is relatively easy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by laserlight View Post
    Try writing it with an int return type first. Changing it to use the reference parameter as an out parameter is relatively easy.
    Having no returns anywhere in the program is whats hanging me up, and the fact that you can't call it like you can with a return type because you can't make the call I noted in the original post when m & n are >0.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    if(m> 0 && n > 0)
    { 
       int tempRes = 0;
       acker (m, n - 1, tempRes);
       acker (m-1, tempRes,result);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sparrowhawk
    Having no returns anywhere in the program is whats hanging me up, and the fact that you can't call it like you can with a return type because you can't make the call I noted in the original post when m & n are >0.
    Compare:
    Code:
    int factorial(int n)
    {
        assert(n >= 0 && "Factorial operand must be non-negative.");
        if (n == 0)
        {
            return 1;
        }
        else
        {
            return n * factorial(n - 1);
        }
    }
    to:
    Code:
    void factorial(int n, int& result)
    {
        assert(n >= 0 && "Factorial operand must be non-negative.");
        if (n == 0)
        {
            result = 1;
        }
        else
        {
            int temp;
            factorial(n - 1, temp);
            result = n * temp;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Yeah I see where you're both going with this... and btw Vart you nailed it, that worked like a charm.

    I just need to think about it for awhile I guess. The whole using a temp variable thing is throwing me. It makes a new temp=0 everytime a new call is made where m & n are >0 from that original call... So is it just a way to make the call possible, or does it have other significance?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM