Problem with C ++ program, does not recognize data types well

2

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.

    
asked by d3vcho 21.10.2018 в 11:54
source

1 answer

2

Each type of data can store a range of certain values.

In the case of int , which typically occupies 32 bits (16 in some architectures), it leaves us 31 bits to store the numbers (1 bit is used for the sign). This gives us a total of:

2^31 = 2.147.483.648

That is, the largest number you can store in a int is lower than the number you intend to store ( 2.824.131.580 ).

You have two possible solutions:

  • If negative numbers are not going to be used, you can use unsigned int . Since this type does not need a sign, you will double the value range 4.294.967.296 .

  • Instead of rushing so much, change the data type. Modern C ++ standards provide a 64-bit type, long long , and its corresponding unsigned, unsigned long long . With these types you should not have problems storing the numbers you are using.

  • answered by 21.10.2018 / 12:15
    source