beginner loop help

This is a discussion on beginner loop help within the C++ Programming forums, part of the General Programming Boards category; this is the code i have for my program. i'm having trouble keeping the output in integer format since i'm ...

  1. #1
    Registered User helloalyssa's Avatar
    Join Date
    Sep 2010
    Posts
    25

    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
    If you start with 200 jackalopes and wait 1 generations,
    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
    If you start with 132 jackalopes and wait 2 generations,
    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
    If you start with 40 jackalopes and wait 100 generations,
    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;
    
    }
    Last edited by helloalyssa; 10-25-2010 at 01:15 PM.

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    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.

  3. #3
    Registered User helloalyssa's Avatar
    Join Date
    Sep 2010
    Posts
    25
    so i posted this yesterday and had my initial questions answered by swarvy (thanks!) but now i'm having other problems. :S

  4. #4
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    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.
    Last edited by Swarvy; 10-25-2010 at 10:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  2. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 09:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21