For Input
4
15.01
15
3
3.01
The value of 'avg' is becoming 9.00. I don't understand why this should be 9.01. Please help.
#include<stdio.h>
#include<math.h>
double round(double x)
{
return floor(x * pow(10.0,2.0) + 0.5)/pow(10.0,2.0);
}
int main()
{
//freopen("10137.txt","r",stdin);
//freopen("out1.txt","w",stdout);
long i,n;
double sum,avg,sum1,a[1005];
while(scanf("%ld",&n) == 1 && n)
{
sum = 0.0;
for(i = 0 ; i < n ; i++)
{
scanf("%lf",&a[i]);
sum += a[i];
}
avg = round(sum/n);
sum1 = sum = 0.0;
for(i = 0 ; i < n ; i++)
{
if(a[i] > avg)
sum += a[i] - avg;
else
sum1 += avg - a[i];
}
if(sum < sum1)
printf("$%0.2lf\n",sum);
else
printf("$%0.2lf\n",sum1);
}
return 0;
}