读取来自数据文件的5个整数求平均值,保留两位小数。编译时没错,怎么结果出错??
#include<stdio.h>int main()
{
int arr[5];
int i=0;
int sum=0;
float average;
FILE *fp;
fp =fopen("data.in","r");
if(fp == NULL){
fprintf(stderr,"can't open file!\n");
fclose(fp);
return 1;
}
while(fscanf(fp,"%d",&arr[i])==1);
i++;
sum=arr[0]+arr[1]+arr[2]+arr[3]+arr[4];
average = sum/5;
printf("%.2f\n",average);
return 0;
}