I have to make a program that determines the population of a certain country after a certain number of years, the years that have to pass so that at that rate it reaches at least twice the initial population, and the population after those years. For this, I have to pass the following values to the function:
- Initial Population
- Birth rate
- Death rate
- Immigration rate
- Number of years
First of all I declare all the variables that I will need:
int pob_inicial, tasa_natalidad, tasa_mortandad, tasa_inmigracion, pob_final, anios;
long long pob_doble=0, pob_trans=0;
const int MAX=50;
bool esMayor = true;
int p[MAX], c[MAX];
Next, I read the necessary values, with their specific filters:
do{
cout << "Introduzca la poblacion inicial: ";
cin >> pob_inicial;
}while(pob_inicial <=0);
pob_doble = pob_inicial*2;
do{
cout << "Introduzca la tasa de natalidad: ";
cin >> tasa_natalidad;
}while(tasa_natalidad <= 0 && tasa_natalidad >= 1000);
do{
cout << "Introduzca la tasa de mortandad: ";
cin >> tasa_mortandad;
}while(tasa_mortandad <= 0 && tasa_mortandad >= 1000);
do{
cout << "Introduzca la tasa de inmigracion: ";
cin >> tasa_inmigracion;
}while(tasa_inmigracion <= 0 && tasa_inmigracion >= 1000);
do{
cout << "Introduzca el numero de años: ";
cin >> anios;
}while(anios < 0);
Now, I calculate the population passed after the years previously introduced (this loop does it well):
p[0] = pob_inicial;
// Calcular la población transcurridos los años
for(int i=1; i<=anios; i++){
p[i] = p[i-1] + ((p[i-1]/1000)*tasa_natalidad) - ((p[i-1]/1000)*tasa_mortandad) + ((p[i-1]/1000)*tasa_inmigracion);
}
pob_final = p[anios];
Now, I propose the loop to calculate twice the initial population:
c[0] = pob_inicial;
int contador = 1;
// Calcular el doble de la población inicial
while(c[contador] >= pob_doble){
c[contador] = c[contador-1] + ((c[contador-1]/1000)*tasa_natalidad) - ((c[contador-1]/1000)*tasa_mortandad) + ((c[contador-1]/1000)*tasa_inmigracion);
contador++;
}
Finally, according to the counter, I make a loop for the population after those years:
p[0] = pob_inicial;
// Calcular la población transcurrida
for(int i=1; i<=contador; i++){
p[i] = p[i-1] + ((p[i-1]/1000)*tasa_natalidad) - ((p[i-1]/1000)*tasa_mortandad) + ((p[i-1]/1000)*tasa_inmigracion);
}
pob_trans = p[contador];
The problem is that for some reason, the variable pob_doble takes it as negative. I cout that variable, and it gets me a negative value. I tried to change the loops, change the types of variables and so on. A desired value would be to enter the following data: 1375570814 32 12 7 3 (respectively) and obtain 1490027497 (population passed after the years), 27 (number of years to reach the double population) and 2824131580 (the population after those years). / p>
I've been stuck for a few days. I do not know what else to play and I do not know how to solve it.