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 ) ;
}