I can code things okay, but I have real trouble coming up with algorithims to solve problems. For instance this problem:

'A scientist uses rabbits (yes, another rabbit problem) in her experiments. She starts out with one pair(male,female). At the end of each month, a pair of rabbits produces one pair of offspring(also male,female). The scientist has 500 cages to hold her rabbits. Each cage holds one pair of rabbits. Assuming no rabbits ever die, when will she run out of cages?'
The program should printout how the rabbits multiply and produce baby rabbits which then add to the total rabbit population. Then when the total pairs reach past 500 cages print out a message saying in what month that happens. For example:

Month Adults  Babies   Total
1        1         0           1
2        1         1           2
3        2         1           3
4        3         2           5
*snip*
12    144      89         233

Will run out of cages in month xx...

Really, I don't know where to begin other than making the first declarations:

const int CAGE = 500; // I dont know if i need this
int adultRabbits;
int babyRabbits;
int totalRabbits = adultRabbits + babyRabbits;
int month;
for 

cout << "\n" << month << "\t" << adultRabbits << "\t" << babyRabbits << "\t" << pairs; // I know i need this to print the results right

I am thinking I need a loop, but not sure which one to use, For, While, Do, or If. And I definately don't know what math I may need to get the algorithim. Several different ones I suppose.

Any help is greatly appreciated.

Is that table correct? I would think that you should start with two rabbits, then the number should double each month (i.e., exponential growth of 2^x)

Yes the table's correct. I guess at the end of the month, 'babies' can't make babies, so they are not included in the rabbit making population. Hence one of my problems. Somehow at the _end_ of month one, the adults make another pair of rabbits. So there are two pair now, but only one is mature. So next month, at the end, there will be 2 pair that are adults and 1 pair of babies. Then, month 3 has the 3 pair of adults (2 pair + 1 pair of matured babies), which produce 3 more pair. So add the 3 pair of adults + 3 pair of babies. And so on, make sense?

Even if it were exponential, can ya start me in the right direction as to setting up the code? I'm pretty sure its a IF loop, then need the ++increment, but not sure where to finish it.

First thing you probably want to do is write out more of that table (incidentally, I just recognized that sequence). Once you can describe the pattern, it shouldn't be too hard to replicate it.

Also, you should be able to use any type of loop if you set it up correctly, but in this case I believe a while or a for loop would work best.

I've got the first workings of code of the for loop, but thats it:

for (int currentMonth = 0; currentMonth < 15; currentMonth++){
cout << "\n" << currentMonth << "\t" << adultRabbits << "\t" << babyRabbits << "\t" << totalPairs;
}

So I can print out the table fine, i just need the algorithim and I can't figure it out :(

Its something like adultRabbits + babyRabbits = totalRabbits;

Edited 3 Years Ago by happygeek: fixed formatting

Hmm, this seems to work for adding the totals up:

for (int currentMonth = 0; currentMonth < 15; currentMonth++){
adultRabbits = adultRabbits + 1;
babyRabbits = adultRabbits + 1;
int totalPairs = adultRabbits + babyRabbits;
cout << "\n" << currentMonth << "\t" << adultRabbits << "\t" << babyRabbits << "\t" << totalPairs;
}

But I can't get the right math to take me to the correct totals :/

Edited 3 Years Ago by happygeek: fixed formattng

Well, I gues I can try to give you some hints. The following should go inside the loop

First of all, every month the babies age's

adultRabbits += babyRabbits;

Then you get yourself some new baby's

babyRabbits = adultRabbits;//since every copple produce an copple

Then you add to the month and calculate total

month++;
total = adultRabbits + babyRabbits;

Then you can print it out

cout << "\n" << month << "\t" << adultRabbits << "\t" << babyRabbits << "\t" << total;

Then check wether they are with more then 500

if (total > 500){break;}

makes the total code:

while(true)
{
    adultRabbits += babyRabbits;
    babyRabbits = adultRabbits;//since every copple produce an copple
    month++;
    total = adultRabbits + babyRabbits;
    cout << "\n" << month << "\t" << adultRabbits << "\t" << babyRabbits << "\t" << total;
    if (total > 500){break;}
}

I haven't tested or debugged this so it could contain bugs. I hope you get the general idea though.

Thats awesome that helps a lot. Thanks! If you see the table above, you can see that it is a little different though, so i am thinking that adultrabbits != babyrabbits, because in the first month there is only 1 adult pair and no babies yet.

So i am thinking that adultrabbits != babyrabbits, because in the first month there is only 1 adult pair and no babies yet.

I don't get what you are saying here.

Well you have adultrabbits += babyRabbits;
I'm not sure this works, because in the first month adults are 1 and babies are 0; The 2nd month then has 2 adults and then 1 babypair. So there needs to be a way to increment or decrement maybe? Basically you are moving 1 pair of babies over to the adultrabbit column at the end of the month. It's confusing huh? :/

All of these are in terms of pairs by the way.

Ow, sorry. I assumed(I didn't wrote that right, did I?) that you would start with 2 adultrabbits.

And about the incrementing, you mean having one pair of rabbit's moved over to adults, I thought all babie's aged after an month (must be some weird kind of rabbit then). The code for that would be:

if (month != 1){adultrabbit += 1; babierabbit -= 1;}

At least, if I understood what you are trying to say correctly.

Think about it this way (ignore the total for now):

End of Month 1: Adults - 1, Babies - 0
End of Month 2: Adults - 1, Babies - 1
End of Month 3: Adults - 2, Babies - 1 <-- Number of adults _last_ month
End of Month 4: Adults - 3, Babies - 2

What you're doing is setting babies to the number of adults _this_ month. To get the correct number, you should subtract what you added last month from the current number of adults.

Also, you should set your initial values of adults, babies, etc. Then inside the loop, output first, then perform your calculations.

BTW, your output is wrong. You check when the adults run out of cages, but you should check when the total runs out of cages, at least, I think you should. Babies to take cages :P

Just wanted to say thanks for the great help so far!

So i need to do that for ever month? or you are using that code as an example? There is supposed to be a loop that continues until you hit 500 pairs and then printout the month in which you hit that number.
So writing a line for each month seems unlikely since you don't know (technically) when you will hit that number?

BTW, your output is wrong. You check when the adults run out of cages, but you should check when the total runs out of cages, at least, I think you should. Babies to take cages :P

Hehe, that is the correct output. Don't ask me why, I think its silly, but it's what the instructor wants so I need to code it to look like that :(

Here is what I now have:

using namespace std;

    int currentMonth = 0;
    int adultPairs = 1;
    int babyPairs = 0;
    int totalPairs;
    
int main()
{

        cout << "\nTable of rabbit population, in pairs, for 12 months" << endl;
        cout << "\n" << "Month" << "\t" << "Adults" << "\t" << "Babies" << "\t" << "Total";

while((adultPairs + babyPairs) <= 500)
    {
        adultPairs += babyPairs; // babies grow to be adults
        currentMonth++;                     // at the end of the month
        babyPairs = adultPairs;    // all adults have 1 pair of babies
        totalPairs = babyPairs + adultPairs;
        
        cout << "\n" << currentMonth << "\t" << adultPairs << "\t" << babyPairs << "\t" << totalPairs;

   }
    if (totalPairs > 500){
        cout << "\nWill run out of cages in month " << currentMonth << endl;
        
       }

     system("PAUSE");
    
     return 0;
}

What I have now, is not printing the right totals. Its close though. What do I need to change to get the ouput like how it is in the image I posted?

What you're doing is setting babies to the number of adults _this_ month. To get the correct number, you should subtract what you added last month from the current number of adults.

How can I do this? I've tried various ways like adultRabbits -= babyRabbits and vice versa to no avail.

This article has been dead for over six months. Start a new discussion instead.