Problems in C , storing several different values and doing arithmetic media

Asked

Viewed 229 times

0

I have a doubt, I have a programming work to do, but the problem is somewhat complex.

My question is: How do I store several different values and do arithmetic media.

I was able to calculate the average of 2 values , but when I type more than one , he mocks the average , someone knows tell me what’s wrong?

Just follow my code :

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

int main(){
    float salario,inss,bcalculo,aliquota,deducao,ir,cont=0,cont2=0,cont3=0,aux,aux2,aux3,mediasal,mediadep,mediair,vsal,soma,x;
    int dependentes;
    aux=0;
    aux2=0;
    aux3=0;

    while(1){
    vsal=salario;
    printf("Digite o salario:\n");
    scanf("%f",&salario);
    soma=salario+vsal;
    aux+=salario;
    if(salario<=0){
        break;
    }   
    if(salario<=1751.81)
    {
        inss=salario*0.08;
        printf("%.2f\n",inss);
        cont++;
    }
    else if(salario>=1751.82 && salario<=2919.72)
    {

        inss=salario*0.09;
        printf("%.2f\n",inss);
        cont++;
    }
    else if(salario>=2919.73 && salario<=5839.45)
    {

        inss=salario*0.11;
        printf("%.2f\n",inss);
        cont++;
    }
    else{

        inss=642.34;
        printf("%.2f\n",inss);
        cont++;
    }   
    aux=salario;
    printf("Digite o numero de dependentes:\n ");
    scanf("%d",&dependentes);
    bcalculo=salario-inss-(dependentes*189.59);
    printf("%.2f\n",bcalculo);
    if(bcalculo<=1903.98)
    {
        aliquota=1;
        deducao=0;
        ir=bcalculo*aliquota-deducao;
        printf("%.1f\n%.2f\n%.2f\n", aliquota,deducao,ir);
        aux2=ir;
    }
    else if(bcalculo>=1903.99 && bcalculo<=2826.65)
    {
        aliquota=0.75;
        deducao=142.80;
        ir=bcalculo*aliquota-deducao;
        printf("%.1f\n%.2f\n%.2f\n", aliquota,deducao,ir);
        aux2=ir;
    }
    else if(bcalculo>=2826.66 && bcalculo<=3751.05)
    {
        aliquota=0.15;
        deducao=354.80;
        ir=bcalculo*aliquota-deducao;
        printf("%.1f\n%.2f\n%.2f\n", aliquota,deducao,ir);
        aux2=ir;
    }
    else if(bcalculo>=3751.06 && bcalculo<=4664.68)
    {
        aliquota=0.225;
        deducao=636.13;
        ir=bcalculo*aliquota-deducao;
        printf("%.1f\n%.2f\n%.2f\n", aliquota,deducao,ir);
        aux2=ir;
    }
    else{
        aliquota=0.275;
        deducao=869.36;
        ir=bcalculo*aliquota-deducao;
        printf("%.1f\n%.2f\n%.2f\n", aliquota,deducao,ir);
        aux2=ir;
    }
    if(cont>1)
    {
    mediasal=soma/cont;
    }
    printf("media salarios\n");
    printf("%.2f\n", mediasal); 
 }



    return 0;
 }
  • then , but this goes within a "for" for example ?

  • because I need her to store the amount of salary sum every time a new one is typed.

  • I think I got it , then I’ll have to use a while for each of the media types , but as for the structure of the code , goes one inside the other?

  • Use a single loop (for, while or ... while) and within the loop calculate the desired amounts and amounts. After the loop calculate the averages.

  • then I can put all my code inside a loop?

  • @anonymity can help me?

Show 1 more comment

1 answer

0

Complementing your code:

#include <stdio.h>
int main(){
    float salario, inss, bcalculo, aliquota, deducao, ir, soma_sal=0, soma_ir=0;
    int dependentes, soma_dep=0, qtd=0, i, faixa_ir[5]={0, 0, 0, 0, 0};
    printf("Informe a quantidade de funcionários: ");
    scanf("%d", &qtd);
    while (qtd <= 0) {
        printf("Precisa ser maior que zero. Reinforme a quantidade de funcionários: ");
        scanf("%d", &qtd);
    }
    for (i=1; i<=qtd; i++) {
        printf("Informe o salário do %dº empregado: ")
        scanf("%f", &salario);
        soma_sal += salario;
        if (salario <= 1751.81) {
            inss = salario*0.08;
        }
        else 
            if (salario <= 2919.72) {
                inss = salario*0.09;
            }
            else 
                if (salario <= 5839.45) {
                    inss = salario*0.11;
                }
                else {
                    inss = 642.34;
                }
        printf("\tINSS: %.2f\n", inss);
        printf("\tInforme a quantidade de dependentes do %dº funcionário: ", i);
        scanf("%d", &dependentes);
        soma_dep += dependentes;
        bcalculo = salario - inss - (dependentes * 189.59);
        printf("\tBase de cálculo: %.2f\n",bcalculo);
        if (bcalculo <= 1903.98) {
            aliquota = 0;
            deducao = 0;
            faixa_i[0]++;
        }
        else
            if (bcalculo <= 2826.65) {
                aliquota = 0.75;
                deducao = 142.80;
                faixa_ir[1]++;
            }
            else 
                if (bcalculo <= 3751.05) {
                    aliquota = 0.15;
                    deducao = 354.80;
                    faixa_ir[2]++;
                }
                else
                    if(bcalculo <= 4664.68) {
                        aliquota = 0.225;
                        deducao = 636.13;
                        faixa_ir[3]++;
                    }
                    else {
                        aliquota = 0.275;
                        deducao = 869.36;
                        faixa_ir[4]++;
                    }
        ir = bcalculo * aliquota - deducao;
        soma_ir += ir;
        printf("\tIR: %.1f%%\t%.2f\t%.2f\n", aliquota*100, deducao, ir);
    }
    printf("\n\nResumo:\n");
    media_sal = soma_sal / qtd;
    printf("\tMédia dos salários: %.2f\n", media_sal);
    printf("\tNúmero médio de dependentes: %.2f\n", (float) soma_dep / qtd);
    printf("\tQuantidade por faixa de IR:\n");
    for (i=0; i<5; i++)
        printf("\t\t%d - %d\n", i, faixa_ir[i]);
    return 0;
}

Note that the code can be greatly improved.

  • Thanks for helping , I had not thought to use vectors , so I was declaring too many variables , I will study what you did . It may even be that in the background it’s simple this , but as I’m starting , I’m always half lost.

  • Even to store the values and aliquots of INSS and IR the use of vectors can make your code more compact.

  • Yes, I hadn’t even remembered him, both because I hardly use him

Browser other questions tagged

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