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

have you tried n=0.0 or making it a double too

well when i add you number together and i dived by 5 I'm getting 8.004. so with your round function you would have ((8.004 * 100) +.5) / 100 = (8.4 + .5) / 100 = 8.9 / 100 = 8.009. with the floor of 8.009 being 8. I'm not sure why you are doing this though. also with double values the last digit can become a one because it is an approximation of the number unlike an int where it is the number.

my intention was to compare two double numbers. Is there any better ideas how I can do that? Because you cant do just
if(a>b) then.....
where a and b are both double.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.