Continuation of a program in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abrown07
    New Member
    • Jan 2010
    • 27

    Continuation of a program in C

    Hi I have the following code:



    #include<stdio. h>
    #include<math.h >

    int main(void)

    {

    double Population,capa city,initial;
    int generations;
    float growth;


    printf("Please give the carrying capacity of the environment: ");
    scanf("%lf", &capacity);

    printf("Please give the intrisic growth rate: ");
    scanf("%f", &growth);


    printf("Please give the inital population of the species: ");
    scanf("%lf", &initial);


    Population=init ial*exp (growth)*(1-(initial/capacity));

    printf("The population at the next generation will be: %.2f\n", Population);



    initial=Populat ion;

    printf("Please indicate how many generations you wish to calculte: ");
    scanf("%d", &generations );

    for(generations =2; generations<=80 ; initial==Popula tion)
    {

    Population+=Pop ulation*exp (growth)*(1-(Population/capacity));

    printf("The population at the next generation will be: \n");

    }

    return(0);

    }



    and I cannot get my loop to function correctly!! I need to make it so that the loop performs the calculations for generations 2 through 80 each time using the previously calculated value of Population as the "initial" variable in the next calculation and then how to break it once it has calculated the population at the 80th generation?
  • YarrOfDoom
    Recognized Expert Top Contributor
    • Aug 2007
    • 1243

    #2
    It might help if the generation variable would actually change during that for loop. Please read up on for-loops to make sure you fully understand how to work with them.

    PS: It is much easier for people to read the code in your posts if you use code-tags (put [code] in front of your code block and [/code] behind it).

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      try:
      when you get the for loop sorted out calculate the population for that iteration and then get the new total with population+= population
      BTW your code is too complex and you are trying to put 5 kg in a 1 kg bag.
      ....use a more conventional for loop.

      Comment

      • abrown07
        New Member
        • Jan 2010
        • 27

        #4
        where is the population+=pop ulation supposed to fit in? that is the part i do not understand!

        Comment

        • YarrOfDoom
          Recognized Expert Top Contributor
          • Aug 2007
          • 1243

          #5
          Originally posted by abrown07
          where is the population+=pop ulation supposed to fit in? that is the part i do not understand!
          My guess is he meant something like totalPopulation += populationIncre aseForThisGener ation, so actually to spread out the calculation over multiple statements to make the code easier to read and understand.

          Comment

          Working...