1
I’m having a problem with an average calculation.
I have a struct with 3 fields. The ones I’m looking for are average product quantity and unit value. I need to average all the products and their prices. However, when I have unit values with 0.50 for example, it does not sum in the calculation and averages only the values that start with at least 1.00. The quantity is int and the value is float.
The sum and the average is as follows:
int x;
double soma=0,media;
for(x=0;x<n;x++)
{
soma+=((prod[x].PRECO)*(prod[x].QTDE));
}
media=soma/n;
printf("\n\n\nA media de valor entre todos os produtos estocados eh: %.2lf", media);
This gives the impression of an input error. How do you collect the information for each element of the array? Remember that a type variable
int
cannot have values with floating comma (type0.5
).– pmg
show how you initialize
prod[x].PRECO
– Math
I already noticed the bug. I was actually doing the data input using ',' instead of '.' when breaking the float.
– Vinícius
Which function could use to ensure correct input, even the user using ',' and not give problems in the program?
– Vinícius
To ensure correct entry uses
fgets()
andstrtol()
(and/orstrtod()
) validated!!– pmg