Home Segments Index Top Previous Next

363: Mainline

Thus, you can compile and run the following program:

#include 
// Function prototype for rabbits function:  
int rabbits (int);                           
// Function definitions requiring rabbits function prototype:
int previous_month (int n) {return rabbits (n - 1);}
int penultimate_month (int n) {return rabbits (n - 2);}
// Function definition for rabbits function:               
int rabbits (int n) {                                      
  if (n == 0 || n == 1)                                    
    return 1;                                              
  else return previous_month (n) + penultimate_month (n);  
}                                                          
// Test rabbits function: 
main ( ) { 
  cout << "At the end of month 1, there is "  << rabbits (1) << endl 
       << "At the end of month 2, there are " << rabbits (2) << endl 
       << "At the end of month 3, there are " << rabbits (3) << endl 
       << "At the end of month 4, there are " << rabbits (4) << endl 
       << "At the end of month 5, there are " << rabbits (5) << endl; 
} 
--- Result --- 
At the end of month 1, there is 1 
At the end of month 2, there are 2 
At the end of month 3, there are 3 
At the end of month 4, there are 5 
At the end of month 5, there are 8