Question: CSULB - CECS 3 2 5 Section 1 & 2 System Programming Program # 5 - goldRabbits: using maps I got a pair

CSULB-CECS 325 Section 1\& 2 System Programming Program \#5- goldRabbits: using maps
I got a pair of rabbits for my birthday. I did some research and found out that apparently rabbits multiply like... well, Rabbits! So I have a great idea that will help me retire. I'm going to start a rabbit ranch. I need to plan for the future and determine how much land and rabbit food I will need for my rabbits.
Here are the Rabbit Rules:
- New-born rabbits take one month to mature.
- One pair of mature rabbits will have a pair of baby rabbits after one month.
- Mature rabbits will continue to produce a new pair of baby rabbits every month.
- Rabbits never die
I plan to retire in 10 years. How many rabbits will I have by then?
I did some hand calculations and I came up with this:
At the start of month \(0, I \) receive the baby rabbits so I have \(\mathbf{1}\) pair of rabbits.
At the start of month 1, the baby rabbits are grown and the female becomes pregnant. But I still have only 1 pair of rabbits.
At the start of month 2, the first pair give birth to 1 pair of rabbits, so now I have \(\underline{2}\) pairs of rabbits. The first pair (the adults) immediately become pregnant again.
At the start of month 3, the second pair is now mature and the first pair give birth again to 1 pair of rabbits. This gives me \(\mathbf{3}\) pairs of rabbits.
At the start of month 4, the first pair gives birth to a new pair, the second pair gives birth to a new pair, and the third pair are all grown up and ready to start family life. So now I have \(\underline{\mathbf{5}}\) pairs of rabbits.
By now I was getting confused with counting rabbits, but I noticed a pattern. It seemed like each new month the total number of pairs of rabbits was equal to the total number from the month before plus the total number from 2 months before. That's really cool that I can predict the number of rabbit pairs with an equation.
I'm gonna call it MGRCFRRPG or "Master Gold's Really Cool Formula for Rapid Rabbit Population Growth", or just GoldRabbits for short. (Apparently some Italian dude from the year 1202 named Fibonacci came up with something similar - but I think mine is much more better...)
As you can see, I have a couple problems:
For some reason, the number got real big - it was approaching 2,147,483,647 at
goldRabbits(45)-(Actual value was 1,836,311,903 when it suddenly went negative at
goldRabbits(46)... That's weird. Am I right? What is going on????
It's taking a long, long time to calculate. At this rate, I will be retired before the
calculation is complete...
Your assignment is to solve these problems for me:
Rewrite the goldRabbits(int n ) function so that it throws an exception if it detects integer
overflow. The exception will throw a string that indicates the input number (n) which
caused the overflow.
Speed up the goldRabbits function so that it is very fast. You can use a static map to
accomplish this.
Keep track of how many times the goldRabbits function is called. You did something
similar in Prog 4 to track of global swaps.
The fibo values are stored in a map in the goldRabbits function. If the incoming parameter
is -1, then the function will print the contents of the map. Example: goldRabbits(-1) will
print the map. Both first> and values will be printed - refer to the sample
output below.
CSULB - CECS 3 2 5 Section 1 \ & 2 System

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!