Ackermann's Function Exercise

Ackermann's function is an important result in theoretical computation. It is not, as some students believe, simply a way to make students perform useless computations. The function is recursive and given as

              |  n+1 		    if m = 0
    ACK(m,n) =|  ACK(m-1,1)	    if m>0 and n = 0
              |  ACK(m-1, Ack(n-1)) otherwise

where both m and n are non-negative.

For this exercise we will write a program which will compute values for Ackermann's function for 0≤ m ≤ 3 and 0 ≤ n ≤ 4. If you look at the article on wikipedia, you will see why this limit was chosen.

A first step would be to write the c++ code to compute this table:

#include <iostream>
#include <iomanip>

using namespace std;

int Ack(int m, int n);

const int M_LIMIT = 3;
const int N_LIMIT = 4;

int main() {
   int m,n;

   cout << "m\\n";
   for(n=0;n<= N_LIMIT; n++) {
       cout << setw(4) << n; 
   }
   cout << endl;

   for(m=0; m<= M_LIMIT; m++) {
       cout << setw(4) << m;
       for(n=0;n<= N_LIMIT; n++) {
          cout << setw(4) << Ack(m,n);
       }
       cout << endl;
   }

   return 0;
}

int Ack(int m, int n) {
   if (m == 0) {
      return n+1;
   } else if (n == 0) {
      return Ack(m-1,1);
   } else {
      return Ack(m-1, Ack(m,n-1));
   }
}
This code is available here

We do quite a bit of cout << setw(4) << val so I want to write a function to do this, sort of.

Implementing Ackermann's function To receive credit for this assignment, assemble the pieces of Ackermann's function in your code, make sure it works and submit the final program to your instructor as an attachment to an email.

If you are completely confused or totally lazy, this file might be helpful.