CSCI 262 Data Structures--Spring 2004

    Home | | Syllabus | | Assignments | | Lecture Notes

    Assignment 4 Solutions

    Solution ackermann.cc code:

    /*******************************************************************
    * Name: Brenda Student
    * Date: 3/15/04
    * Course: CSCI 262 Section 01
    * Assignment: Assignment 4
    * Instructor: Royden
    * Program: ackermann.cc
    * Purpose:  A program that finds the output of the ackermann function
    * for two integer inputs, m and n.
    ***************************************************************************/
    #include <iostream.h>
    int ackerman(int, int);
    
    int main(void) {
      int n, m;	//input integers
      
      cout << "Enter positive integer for m: ";
      cin >> m;
    
      cout << "Enter positive integer for n: ";
      cin >> n;
    
      cout << "The ackerman value is " << ackerman(m, n) << "." << endl;
    }
    
    /*************************************************************************
    * int ackerman(int, int)
    * Purpose: A recursive program to compute the output of the Ackermann function.
    * Pre: m and n are positive integers
    * Post: returns the value of the ackerman function for inputs m and n
    **************************************************************************/
    int ackerman(int m, int n) {
      if ((m == 0) ) {
        return n + 1;
      } else if (n == 0) {
        return ackerman(m - 1, 1);
      } else {
        return ackerman(m-1, ackerman(m, n-1));
      }
    }
    
    

    Home | | Syllabus | | Assignments | | Lecture Notes