You are here:

- Home
- Computing/Technology
- C/C++
- C++
- C++ programme

Advertisement

Write a single recursive function in C++ that computes Ackerman's function. Do not use

other functions or loops. Assume parameters m and n passed are always greater or equal to zero.

int ackerman(int m, int n) { }

{

}

Ackerman's function is defined as follows:

A(0, j)=j+1 for j ¡Ý 0

A(i, 0)=A(i-1, 1) for i > 0

A(i, j)=A(i-1, A(i, j-1)) for i, j > 0

To implement it, you need to consider thre cases. Two cases are special cases (when i is zero, or when j is zero). The other case is general (when i and j are not zero).

In recursive functions, the function gets called from inside. For instance:

int ackerman(int m, int n)

{

if(m != 0 && n != 0){

return ackerman(m-1, n-1);

}

In recursive functions, you should always take care of the special cases separately.

I hope this helps. Please let me know if you need further assistance with this concept. If you need assistance with coding recursive functions, you may email recursion@shabah.net. They can help you with coding and implementation issues.

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | thank you for all everthing |

You may ask me about simple to complicated C/C++ programming methods/style and most like questions about network programming in C++.

I'm a professional programer in C++ and network/internet programming.