Ackermann Function w/ a twist.

This is a discussion on Ackermann Function w/ a twist. within the C++ Programming forums, part of the General Programming Boards category; function header: void acker (int m, int n, int &result) There's no return type so I can't return things for ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    21,451
    Try writing it with an int return type first. Changing it to use the reference parameter as an out parameter is relatively easy.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Code:
    if(m> 0 && n > 0)
    { 
       int tempRes = 0;
       acker (m, n - 1, tempRes);
       acker (m-1, tempRes,result);
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    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;
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07: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

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