Advertisement
DrAungWinHtut

cell.c

Oct 10th, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>  // for rounding
  3.  
  4. int main() {
  5.     int population;
  6.    
  7.     // Input validation for initial population
  8.     do {
  9.         printf("What is the initial population? ");
  10.         scanf("%d", &population);
  11.         if (population < 100 || population > 500) {
  12.             printf("The initial population given is incorrect\n");
  13.         }
  14.     } while (population < 100 || population > 500);
  15.    
  16.     int initial_population = population;
  17.     double total_population = 0;
  18.     int days = 0;
  19.    
  20.     // First part: 50 days cycle of feeding and splitting
  21.     for (int i = 1; i <= 50; i++) {
  22.         if (i % 3 == 0) {
  23.             // Split population every third day
  24.             population = (int) round(population * 0.5);
  25.         } else {
  26.             // Feed population, increasing by 66%
  27.             population = (int) round(population * 1.66);
  28.         }
  29.         total_population += population;
  30.         days++;
  31.     }
  32.    
  33.     printf("The population after 50 days is %d\n", population);
  34.    
  35.     // Second part: population decreases by 16% until it falls below 100
  36.     while (population >= 100) {
  37.         population = (int) round(population * 0.84);
  38.         total_population += population;
  39.         days++;
  40.     }
  41.    
  42.     printf("After %d days the population will return below 100\n", days);
  43.    
  44.     // Calculate average population
  45.     double average_population = total_population / days;
  46.     printf("On average there were %.2f cells in the colony\n", average_population);
  47.    
  48.     return 0;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement