How to use input from first operation as variable in second operation

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

    How to use input from first operation as variable in second operation

    How do I get the program to take the "Population " calculated from the first time and use it as the "initial" variable for the next calculation. And for it to continue to do this for generations 2 on each time taking the previously calculated Population and using it as the variable "initial" for the next calculation. With what I have right now when I run the program it just kind of... pauses after I give all the inputs.

    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main(void)
    
    {
    
      double Population,capacity,initial;
      int generations;
      float result,growth;
      
      
       printf("Please give the carrying capacity of the environment: ");
       scanf("%lf", &capacity);
       
       printf("Please give the intrisic growth rate: ");
       scanf("%f", &growth);
       
       result=exp (growth);
       
       
       printf("Please give the inital population of the species: ");
       scanf("%lf", &initial);
       
       printf("Please indicate the number of generations: ");
       scanf("%d", &generations);
       
       Population=initial*result*(1-(initial/capacity));
     
        for(generations = 2; generations <=80; initial= Population)
       
           Population=initial*result*(1-(initial/capacity));
       
       printf("The expected population at the next generation is: %.6f\n",Population);
       return(0);
       
    }
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Convert population into a function
    //then
    population+=pop ulation();
    BTW you should place you code in between code tags to make it easier for others to read.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      The problem was that in your for loop you had:
      Population=init ial*result*(1-(initial/capacity));
      You're using the initial variable over and over. You wanted:
      Population=Popu lation*result*( 1-(initial/capacity)); or
      Population*=res ult*(1-(initial/capacity));

      Comment

      Working...