Home > Software engineering >  Why am I getting wrong answer? CS50 Lab1 Population
Why am I getting wrong answer? CS50 Lab1 Population

Time:10-07

When running the program by setting initial_population= 100 and ending_population=200 it get 13 years but the correct answer is 9 years

#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void){
    
    int initial_population, births, deaths, sum, ending_population;
    int years=0;
    
    do{
    initial_population = get_int("What is the initial population?");
    }
    while (initial_population < 9);
    
     do{
    ending_population = get_int("What is the ending population?");
    }
    while (ending_population < initial_population);
    
    births = initial_population/3;
    
    deaths = initial_population/4;
        
      
        while(initial_population < ending_population){
            
            initial_population = initial_population   births - deaths;
            years  ;
        }
        
        printf("Years: %d\n", years);
        
    }

CodePudding user response:

births and deaths need to be computed on the current population. This program essentially makes them constants. After initial_population "changes", births and deaths need to "change" too.

  • Related