Fibonacci Rabbits that die

A pair of newly born rabbits (one male, one female) is put in a field. Rabbits are able to mate at the age of one month so that at the end of the second month each pair produces two new pairs of rabbits and then dies.

Note: In month 0, there are 0 pairs of rabbits. In month 1, there is 1 pair of rabbits.

1. Write a program – using a while loop – that takes the number of months from the user and prints the number of pairs of rabbits at the end of that month.

2. In the same cpp file, write a recursive function rabbits() that takes the number of months as input and returns the number of pairs of rabbits at the end of that month.

3. In the main program, call the function rabbits() with the number that the user entered. Output both calculations (i.e. the one you obtained with the loop and the one that the recursive function returns) and see if they are equal.

The description is fairly self explanatory. I already have the main program down, but I can't figure out how to implement the rabbits dying after reproducing.

**** This is my code so far, I don't know how to implement the rabbits dying, can anybody point me in the right direction?
------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;

int rabbits (int);

int main ()
{
    int x, month, result, counter = 0, rab_now, rab_lastmonth = 1,        
    rab_twomonthsago = 0;

    cout << "Please enter the month \n\n";
    cin >> month;
    cout << "\n";
   
    result = rabbits (month);
    
    while (counter <= month - 1)
    {
          rab_now = rab_lastmonth + rab_twomonthsago;
          
          x = rab_lastmonth;
          rab_lastmonth = rab_now;
          rab_twomonthsago = x;
          
          counter++;
    }

    cout << "At the end of month " << month << ", there will be " << 
    rab_lastmonth << " pairs of rabbits" << endl;

    system("PAUSE");
    return 0;
}

int rabbits (int month)

{
    if (month == 0)
    {
        return 0;
    }
    else if (month == 1)
    {
        return 1;
    }
    else
    {
        return (rabbits (month + 1) + rabbits (month - 2));
    }
}
Topic archived. No new replies allowed.