Question: CSULB - CECS 3 2 5 Section 1 & 2 System Programming Program # 5 - goldRabbits: using maps I got a pair
CSULBCECS Section & System Programming Program # 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. Im 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:
Newborn 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 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 I receive the baby rabbits so I have mathbf pair of rabbits.
At the start of month the baby rabbits are grown and the female becomes pregnant. But I still have only pair of rabbits.
At the start of month the first pair give birth to pair of rabbits, so now I have underline pairs of rabbits. The first pair the adults immediately become pregnant again.
At the start of month the second pair is now mature and the first pair give birth again to pair of rabbits. This gives me mathbf pairs of rabbits.
At the start of month 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 underlinemathbf 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 months before. That's really cool that I can predict the number of rabbit pairs with an equation.
Im 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 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 at
goldRabbitsActual value was when it suddenly went negative at
goldRabbits 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 goldRabbitsint 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 to track of global swaps.
The fibo values are stored in a map in the goldRabbits function. If the incoming parameter
is then the function will print the contents of the map. Example: goldRabbits will
print the map. Both first and values will be printed refer to the sample
output below.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
