Division does not generate the expected result

Asked

Viewed 115 times

1

When I do the division of mul / total_filhos_mul the variable mul is zeroed, if I don’t make this division she gets the correct value.

What may be happening?

#include <stdio.h>
int main(){

    int hab,i=0,hom=0,mul,sal_menor=0,total_filhos_mul,sem_filhos=0,soma_sal=0;
    char sexo;
    printf("informe o numero de habitantes:\n");
    scanf("%d",&hab);

    mul =0;
    hom =0;

    int sal[hab];
    int filhos_mul[hab];

    for(i = 0; i < hab; i++){
        printf("\ninforme o valor do salario:");
        scanf("%d", &sal[i]);
        printf("\nquantidade de filhos:");
        scanf("%d", &sem_filhos);
        if(sal[hab] <= 600 && sem_filhos == 0)
            sal_menor=sal_menor + 1;

            printf("\ninforme o sexo (F/M):");
            scanf("%s", &sexo);
            if(sexo == 'm')
                hom = hom + 1;

                if(sexo == 'f'){
                    mul=mul + 1;
                    printf("\nquantidade de filhos:");
                    scanf("%d", &filhos_mul[i]);
                }

                total_filhos_mul = total_filhos_mul + filhos_mul[i];
                soma_sal = soma_sal + sal[i];

                printf("\n\n\n-------#########################################-------\n\n\n");

    }

    int maior, menor;
    maior = sal[0]; 
    menor = sal[0];

    for (i = 0; i < hab; ++i) {
        if (sal[i] > maior){
            maior = sal[i];
        }
        if (sal[i] < menor){
            menor = sal[i];
        }
    }

    printf("\n%d mulheres", mul/*total_filhos_mul*/);
    printf("\n%d total de filhos", total_filhos_mul);

    float media_sal;
    media_sal = hab / soma_sal;
    printf("\nA media do salario da populacao e: %.2f\n", media_sal);

    float media_filhos;
    media_filhos = mul / total_filhos_mul;
    printf("\nA media de filhos das mulheres e: %.2f\n", media_filhos);

    printf("\nO maior salario e: %d\n", maior);
    printf("\nO menor salario e: %d\n", menor);
}
  • Good morning I edited your question to try to make it a little more readable, however there are some parts in your code that I could not perfectly edentar by the lack of {} if you can review.

  • You start mul =0;... if there is no woman, continue with 0. How do you know that mul continue 0 if you do not print this value after performing the calculation (media_filhos = mul / total_filhos_mul;)?

  • I have already initialized Mul =0, but it is reset if I do the division by total_filter_mul, when I do not do this division and give a printf in this variable it returns with the correct value.

  • I print this value at the very end of the @Dalton printf code ("on the average of women’s children and: %.2f n", media_children);

1 answer

3


It is a typing problem. The two variables of the division are integer, so the result is an integer. You need to make a cast for float before making the split.

It was hard to come to this conclusion because the code is barely readable. Declare variables near where it will be used.

I was even going to improve the code, but I saw that there are other problems, or at least it is not clear what the algorithm needs to do and a change can result in more wrong.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.