Ackermann Function w/ a twist.

• 03-23-2009
Sparrowhawk
Ackermann Function w/ a twist.

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.

 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.
• 03-23-2009
vart
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
• 03-23-2009
Sparrowhawk
Quote:

Originally Posted by vart
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.
• 03-23-2009
laserlight
Try writing it with an int return type first. Changing it to use the reference parameter as an out parameter is relatively easy.
• 03-23-2009
Sparrowhawk
Quote:

Originally Posted by laserlight
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.
• 03-23-2009
vart
Code:

```if(m> 0 && n > 0) {   int tempRes = 0;   acker (m, n - 1, tempRes);   acker (m-1, tempRes,result); }```
• 03-23-2009
laserlight
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;     } }```
• 03-23-2009
Sparrowhawk
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?