![]() |
Data Abstraction and Problem Solving with C++Walls and Mirrorsby Frank M. Carrano |
![]() |
c02p113a.cppGo to the documentation of this file.00001 00023 int iterativeRabbit(int n) 00024 { 00025 // Initialize base cases: 00026 int previous = 1; // Initially rabbit(1) 00027 int current = 1; // Initially rabbit(2) 00028 int next = 1; // Result when n is 1 or 2 00029 00030 // Compute next rabbit values when n >= 3 00031 for (int i = 3; i <= n; ++i) 00032 { // current is rabbit(i-1), previous is rabbit(i-2) 00033 next = current + previous; // rabbit(i) 00034 previous = current; // Get ready for 00035 current = next; // next iteration 00036 } // end for 00037 00038 return next; 00039 } // end iterativeRabbit |