Thread: miscalculation?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    miscalculation?

    We were given samples to test our programs. it works on the first sample, but not the next. there must be an error somewhere in my calculations, because sometimes it works, sometimes it doesnt. any help would be GREATLY appreciated, i'm getting really frustrated...
    Code:
    #include <stdio.h>
    #include <math.h>
    main ()
    {
    float size;
    float level;
    float temp;
    float generations;
    float population;
    float max_population;
    float max_pop_generations;
    
    float k_size;
    float k_level;
    float k_temp;
    float max_pop_hours;
    float hours;
    float k;
    float k_rate_of_change;
    
    max_population = 0 ;
    max_pop_generations = 0 ;
    generations = 0 ;
    population = 1 ; 
    
    /* Print title */
    
    printf ("* *Welcome to the population growth program* *\n") ;
    
    /* Get size */
    
    printf ("What is the size of your container (in feet)? " ) ;
    scanf ("%f", &size) ;
    
    if ( size > 8 || size < 1)
    printf (" %f is an unacceptable value for this data. The value must be between 1 and 8.", size ) ; 
    
    
    /* Get nutrient level */
    
    printf ("What is the nutrient level coefficient?" ) ;
    scanf ("%f", &level) ;
    
    if ( level > 3 || level < 0)
    printf ( " %f is an unacceptable value for this data.The value must be between 0and 3.", level ) ;
    
    
    /* Get temperature */
    printf ("What is the incubating temperature (in degrees C.) ?") ;
    scanf ("%f", &temp) ;
    
    if ( temp > 54 || temp < 20)
    printf ( " %f is an unacceptable value for this data. The value must be between20 and 54.", temp ) ;
    
    
    
    /* Calculations for K */
    
    if (size <= 8 && size >= 1)
     k_size = ( 8 - size ) * 0.003 ;
     
     
    if (level <= 3 && level >= 0 )
     k_level = ( 3 - level ) * 0.004825 ;
     
     
    if ( temp >= 20 && temp <= 54 )
     k_temp = abs ( 37 - temp) * 0.001 ;
    k_rate_of_change = 0.005 + k_size + k_level + k_temp ;
    
    /* Calculations */
    
    while ( population > 0 )
    {
      generations++ ;
      k = 0.1 + ( k_rate_of_change * (generations - 1 )) ;
      population = floor(1 * ( pow ( M_E,(( 1 - k ) * generations )) )) ;
      if ( population > max_population)
      {
        max_population = population ;
        max_pop_generations = generations ;
      } 
    }   
    generations--;
    hours = generations * 20 / 60 ;
    max_pop_hours = max_pop_generations * 20 / 60 ;
    
    /* Print results */
    
    printf (" Maximum population reached after how many generations? %.2f \n" , max_pop_generations) ;  
    printf (" Maximum population %.2f\n", max_population ) ;
    printf (" Maximum population reached after how many hours? %.2f \n " , max_pop_hours ) ; 
    printf (" Population reaches 0 after how many generations? %.2f \n" , generations ) ;  
    printf (" Colony lived for %.2f hours\n", hours ) ;
    }

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    How is it that your calculations are off ? When it doesn't work, what exactly happens ?

    Also, this may sound redundant, but you sure you're used the right formulas ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    the first sample is: the size is 8, the level is 3, and the temperature is 37. which outputs all the right answers.
    max population reached after 91 generations
    max population 60863795821......
    max population reached after 30.33 hours
    population reaches 0 after 181 generations
    colony lived for 60.33 hours

    then if i do the 2nd sample: the size is 1, the level is 0, and the temperature is 20.
    everything is wrong except maximum population after generations...its supposed to be
    max population reached after 8 generations
    max population 54
    max population reached after 2.67 hours
    population reaches 0 after 17 generations
    colony lived for 5.67 hours


    its also the same with the 3rd sample, the size is 6, the level is 2. the temperature is 30
    I'm suppose to get
    max. population reached after 20 generations
    max population 11232
    max populations reached after 6.67 hours
    population reaches 0 after 41 generations
    colony lived for 13.67 hours

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Ok, so what are your results when it is wrong ?

    Also, you may want to put some printf's every here and there to check values during execution, that could give you a better idea of what's going on.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    What is the size of your container (in feet)? 1
    What is the nutrient level coefficient?0
    What is the incubating temperature (in degrees C.) ?20

    Maximum population reached after how many generations? 8.00
    Maximum population 53.00
    Maximum population reached after how many hours? 2.67
    Population reaches 0 after how many generations? 16.00
    Colony lived for 5.33 hours



    What is the size of your container (in feet)? 6
    What is the nutrient level coefficient?2
    What is the incubating temperature (in degrees C.) ?20

    Maximum population reached after how many generations? 14.00
    Maximum population 754.00
    Maximum population reached after how many hours? 4.67
    Population reaches 0 after how many generations? 28.00
    Colony lived for 9.33 hours

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    ok, now check you values for k_rate_of_change in each of the cases, and show me them. Are they all in keeping with what you'd expect ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    What is the size of your container (in feet)? 1
    What is the nutrient level coefficient?0
    What is the incubating temperature (in degrees C.) ?20

    k_rate_of_change =0.057475
    Maximum population reached after how many generations? 8.00
    Maximum population 53.00
    Maximum population reached after how many hours? 2.67
    Population reaches 0 after how many generations? 16.00
    Colony lived for 5.33 hours

    * *Welcome to the population growth program* *
    What is the size of your container (in feet)? 6
    What is the nutrient level coefficient?2
    What is the incubating temperature (in degrees C.) ?30

    k_rate_of_change =0.022825
    Maximum population reached after how many generations? 20.00
    Maximum population 11231.00
    Maximum population reached after how many hours? 6.67
    Population reaches 0 after how many generations? 40.00
    Colony lived for 13.33 hours

    i calculated the k_rate_of_change for both manually. they are both correct.

  8. #8
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Ok, you sure you should be flooring upon every iteration ? It seems to me that doing that will result in pretty strong error growth...
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    if i didnt floor it then how could i fix it????/

  10. #10
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    The thing is that I'm thinking that all your calculations are indeed correct, but the problem rather is that by flooring every iteration you lose decimales that may seem trivial then, but become quite important later on.

    Here's an example :

    Say you have 2.01 and you floor that to 2.

    One can see that 2.01^2 = 4.0401 while what you get is 4. Not too bad. Yet.

    But then 2.01^4 = 16.3224, and you get 16. Now you have error .3. Let's continue.

    2.01^16 = 70980.1509, what you get : 65536. Now you have close to 5000 of error. Hmm. That could be a problem.

    And this was only by floorinh .01 more...Imagine if it were to be with 0.5...

    I think you should just floor your answer after all your calculations, instead of every iteration. That should prevent the exponential error.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    i tried flooring it like this:

    Code:
     generations++ ;
      k = 0.1 + ( k_rate_of_change * (generations - 1 )) ;
      population = (1 * ( pow ( M_E,(( 1 - k ) * generations )) )) ;
      if ( population > max_population)
      {
        max_population = population ;
        max_pop_generations = generations ;
      } 
    floor(population) ;
    }
    generations--;
    hours = generations * 20 / 60 ;
    max_pop_hours = max_pop_generations * 20 / 60 ;
    it gives some right answers, but still not all. im not sure where i should be flooring.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So why don't you figure out your calculations on paper before you try to translate them into code? Then you'll know, and knowing is half of the battle!


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Again, you shouldn't floor within your while loop.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed