You are here:

C++/C++ programme

Advertisement


Question
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) { }
{




}  

Answer
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.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Sam

Expertise

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

Experience

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

©2016 About.com. All rights reserved.