What is the error in this activity in c

Asked

Viewed 96 times

1

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

The statement reads as follows::

A city hall did a survey among its inhabitants, collecting data on people’s salaries. The city wants to know:

  • The average wage of the population.
  • Highest salary.
  • The amount 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 made:

#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;
    
}

1 answer

2


The average is equal to the sum divided by the total of elements. You are not tracking this total in the variable quant_sal, that’s your problem.

In addition, there are several possible simplifications in your code. In particular, with a change in the while, we only need to read the salary once and it works even if the first wage 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 summed (soma += salario;), compared to 1000 (if (salario > 1000)), compared to the largest (if (salario > maior)) and counted as a salary (quant_sal++;) if it was not negative (if (salario < 0) break;).

Note that how salario is compared with maior and added in the soma, we have to soma and maior must be the type float, and not int, since salario is float.

There is still one detail on quant_sal == 0 ? 0 : soma / quant_sal. The use of the ternary operator is to avoid division by zero if the first reported salary is already a negative number.

  • Oops, it worked! Only in (larger=0;), when I put in float he did not do the tracking, but I left in int and it worked. Anyway, thank you for taking the time to verify the code. Ended up helping me and teaching me a few little things that I was in doubt.

  • @Huckboy If this answer solved your problem, mark it as accepted by clicking on the " " that is next to it. This will also mark your question as solved/solved. If you are still not satisfied and have any doubts or objections, feel free to comment.

  • rss, I forgot that detail!! Thank you!

Browser other questions tagged

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