What is the error in this activity in c

1

I need the negative values entered by the user not to enter the calculation, and also that this negative insertion is canceled by the program.

The statement reads as follows:

  

A city council has conducted a survey among its inhabitants, collecting data on people's wages. The city wants to know:

     
  • The average salary of the population.
  •   
  • The highest salary.
  •   
  • The number of people with salaries above R $ 1,000.00
  •   

The end of the reading of the data will happen with the entry of a negative salary.

Part of the code I've already done:

#include <stdio.h>
#include <stdlib.h>

int main (void)

{

    int quant_sal=0,salario_1000=0,soma=0,maior=0;
    float salario=1;

    printf("Insira o salario: ");
    scanf("%f",&salario);

    maior=salario;

    while(salario>=1){

        soma=soma+salario;

        if(salario<=0){
            printf("Salario invalido");
        }

        if(salario>1000){
            salario_1000++;
        }

        if(salario>maior){
            maior=salario;
        }




        printf("Insira o salario: ");
        scanf("%f",&salario);

    }

    printf("A media de salario e: %.1f\n",soma/salario);
    printf("O maior salario e: %d\n",maior);
    printf("Salario acima de 1000 e: %d\n",salario_1000);

    system("pause");
    return 0;

}
    
asked by anonymous 19.12.2017 / 19:27

1 answer

2

The mean is equal to the sum divided by the total of elements. You're not crawling this total in the quant_sal variable, that's your problem.

In addition, there are several possible simplifications to your code. Especially with a change in while , we only need to read the salary once and it works even if the first salary entered is negative.

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    int quant_sal = 0, salario_1000 = 0;
    float salario, soma = 0, maior = 0;

    while (1) {
        printf("Insira o salario: ");
        scanf("%f", &salario);

        if (salario < 0) break;

        soma += salario;
        if (salario > 1000) salario_1000++;
        if (salario > maior) maior = salario;
        quant_sal++;
    }

    printf("A media de salario e: %.1f.\n", quant_sal == 0 ? 0 : soma / quant_sal);
    printf("O maior salario e: %.1f.\n", maior);
    printf("Ha %d pessoas com salario acima de 1000.\n", salario_1000);

    system("pause");
    return 0;
}

Note that the value entered by the user will only be added ( soma += salario; ), compared to 1000 ( if (salario > 1000) ), compared to the greater ( if (salario > maior) ) and counted as a salary ( quant_sal++; ) if you do not have been negative (% with%).

Note that since if (salario < 0) break; is compared to salario and summed in maior , we have soma and soma must be type maior , not float , since int % is salario .

There is also a detail in float . The use of the ternary operator is to avoid division by zero if the first wage reported is already a negative number.

    
19.12.2017 / 20:03