Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h> // for rounding
- int main() {
- int population;
- // Input validation for initial population
- do {
- printf("What is the initial population? ");
- scanf("%d", &population);
- if (population < 100 || population > 500) {
- printf("The initial population given is incorrect\n");
- }
- } while (population < 100 || population > 500);
- int initial_population = population;
- double total_population = 0;
- int days = 0;
- // First part: 50 days cycle of feeding and splitting
- for (int i = 1; i <= 50; i++) {
- if (i % 3 == 0) {
- // Split population every third day
- population = (int) round(population * 0.5);
- } else {
- // Feed population, increasing by 66%
- population = (int) round(population * 1.66);
- }
- total_population += population;
- days++;
- }
- printf("The population after 50 days is %d\n", population);
- // Second part: population decreases by 16% until it falls below 100
- while (population >= 100) {
- population = (int) round(population * 0.84);
- total_population += population;
- days++;
- }
- printf("After %d days the population will return below 100\n", days);
- // Calculate average population
- double average_population = total_population / days;
- printf("On average there were %.2f cells in the colony\n", average_population);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement