问题补充:
求一维数组x[N]的平均值,并多所的结果进行四舍五入保留两位小数。
例如:当x[10]={15.6,19.9,16.7,15.2,18.3,12.1,15.5,11.0,10.0,16.0}时,输出结果为:avg=15.030000.
#include
#include
double fun(double x[10])
{
int i;
long t;
double avg = 0.0;
double sum = 0.0;
for (i=0; i<10; i++)
sum+=x[i];
avg = sum/10;
avg = avg*1000;
t = (avg+5)/10;
avg = (double)t/100;
return avg;
}
main()
{
double avg, x[10] =
{15.6, 19.9, 16.7, 15.2, 18.3, 12.1, 15.5, 11.0, 10.0, 16.0};
int i;
printf("\nThe original data is :\n");
for (i=0; i<10; i++)
printf("%6.1f", x[i]);
printf("\n\n");
avg = fun(x);
printf("average=%f\n\n", avg);
}
程序里面
avg = avg*1000;
t = (avg+5)/10;
avg = (double)t/100;
是什么意思,能详细解释一下吗?