0
The exercise needs to be done with while or while, in C language..
A city hall conducted a survey among its inhabitants, collecting data on salary and number of children. The city hall wishes to know: a) average population wage; b) average number of children; c) higher salary;d) percentage of people with salary up to R $ 100,00. The end of the data reading will take place with the entry of a negative salary.
the entire code is working in general, but when the results appear, the number I put to end the loop is added together, the error is probably in the contador_pessoas++;
, but I’ve tried to put inside a if
, but then when I type numbers smaller than 100 the program does not run and ends..
#include <stdio.h>
#include <stdlib.h>
int filhos=0, contador_pessoas=0, contador_pessoas_100;
float salario=0, media_salario=0, media_filhos=0, porcentagem_salario=0, maior_salario=0, porcentagem=0;
main()
{
do
{
printf("Digite o seu salario(para encerrar digite um salario negativo): ");
scanf("%f", &salario);
contador_pessoas++;
media_salario = media_salario + salario;
if(salario <= 100){
contador_pessoas_100++;
porcentagem = (contador_pessoas_100*100)/contador_pessoas;
}
if(salario>maior_salario){
maior_salario = salario;
}else if(salario < 0){
break;
}
printf("Digite o numero de filhos: ");
scanf("%d", &filhos);
media_filhos = media_filhos + filhos;
}while(1);
printf("A media do salario da populacao e: %0.2f\n", media_salario/contador_pessoas);
printf("A media do numero de filhos da populacao e: %0.1f\n", media_filhos/contador_pessoas);
printf("O maior salario e: %0.2f\n", maior_salario);
printf("A porcentagem de pessoas com salario ate 100 reais e de: %0.1f\n", porcentagem);
return 0;
}
Only with that in the percentage part is now always giving a value of 100, Voce knows pq?
– Luciano Balestrin Correa
Corrected counter initialization and percentage calculation location. It would be useful to test whether a valid salary (counter > 0) was reported before calculating the percentage. Note that you are also doing an operation between integers and therefore the result will be integer but when printing assumes that it is a float. Use cast to force floating point operation.
– anonimo