beginner loop help

• 10-24-2010
helloalyssa
beginner loop help
this is the code i have for my program. i'm having trouble keeping the output in integer format since i'm multiplying by floating point numbers. i'm also having a problem with the last output, mine keeps coming up as 281. if someone could help me out that'd be great.

our output should look like this:

How many jackalopes do you have? 200
How many generations do you want to wait? 1
you'll end up with a total of 204 of them.

How many jackalopes do you have? 132
How many generations do you want to wait? 2
you'll end up with a total of 137 of them.

How many jackalopes do you have? 40
How many generations do you want to wait? 100
you'll end up with a total of 291 of them.

Code:

``` // This program calculates population growth using a loop. #include <iostream> #include <iomanip> using namespace std; int main() {   int jacks,  // initial number of jackalopes       gens;    // number of generations   const float birthRate = .03,  // birth rate         deathRate = .01;  // death rate   // Ask user initial jackalope population.   cout << "How many jackalopes do you have? \n";   cin >> jacks;   // Ask user how many generations will be calculated.   cout << "How many generations do you want to wait? \n";   cin >> gens;   int newPop,    // population of jackalopes including births       newPop2;  // population of jackalopes including deaths   newPop = jacks;   cout << showpoint << fixed << setprecision(2);    for (int i = 1; i <= gens; i++)   {     newPop = (newPop * birthRate) + newPop;  // calculate new population of jackalopes     newPop2 = newPop - (newPop * deathRate);  // calculate how many jackalopes die     newPop = newPop2;  // total population   }   cout << "If you start with "<< jacks <<" jackalopes and wait "<< gens <<" generation(s), \n";    cout << "you'll end up with a total of "<< newPop2 <<" of them. \n";   return 0; }```
• 10-24-2010
Swarvy
Isn't this problem basically just a 'compound interest' problem. Where the 'percentage rate' is just the birth rate minus the death rate? (i.e. 3% - 1%). Oh well.

If you are using loops I would suggest doing a for loop because then the number of generations considered is simply the limit of the loop.

E.g.
Code:

```for(i=0; i < num_of_generations; i++) {       /* Calculate new number of jackalopes */       /* Calculate the number which die */       /* Update the variables so that the loop propagates correctly again */ }```
Now it is your turn to come up with the code which goes in the loop :)

Edit: Like I said before. This is basically just a variation on compound interest, in which case it is questionable whether you even need a loop for this - The compound interest problem can be solved exactly, although you may get some rounding errors if num_of_generations is large.
• 10-25-2010
helloalyssa
so i posted this yesterday and had my initial questions answered by swarvy (thanks!) but now i'm having other problems. :S
• 10-25-2010
Swarvy
Quote:

for (int i = 1; i <= gens; i++)
{
newPop = (newPop * birthRate) + newPop; // calculate new population of jackalopes
newPop2 = newPop - (newPop * deathRate); // calculate how many jackalopes die
newPop = newPop2; // total population
}
I suspect one of the problems you are having is related to the second and third lines of the for loop. You are changing 'newPop' and then using the updated value to calculate the number which have died when I think you want to use the old value (the bit in red will probably cause a problem or two). In terms of making your output come out properly you could just typecast your mathematics.

E.g.
Code:

```int newPop; for(int i=0; i < gens; i++) {       newPop = (int)( newPop*(1 + birthRate - deathRate) ); }```
P.S. Typecasting your maths is one way to make your code give integer values. You could use the 'round' function (included in cmath) which rounds floats to the nearest integer (but returns the number in the form of a 'double'). It may be possible for inaccuracies to arise if you don't round after every operation so the above example would become something like this:
Code:

```for(int i=0; i < gens; i++) {     temp = round( newPop*(1 + birthRate) );     newPop = temp - round( newPop*deathRate ); }```
There are many ways to skin a cat so which method you prefer I leave to you.