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 ?
– Noct
because I need her to store the amount of salary sum every time a new one is typed.
– Noct
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?
– Noct
Use a single loop (for, while or ... while) and within the loop calculate the desired amounts and amounts. After the loop calculate the averages.
– anonimo
then I can put all my code inside a loop?
– Noct
@anonymity can help me?
– Noct