2022-05-14 23:40

# SOS，帮我看看吧，不知道哪里错了

#include <stdio.h>
#include <stdlib.h>
double statistics(int *score,int num,int *maxId,int minId);
int main()
{
int score[200],num,maxId,minId;
double average;
scanf("%d",&num);
FILE
fp;
fp=fopen("成绩.txt","r");
for(int i=0;i<num;i++)
{
fscanf(fp,"%d",&score[i]);
}
fclose(fp);
statistics(&score[200],num,&maxId,&minId);
printf("%d %d %lf",maxId,minId,average);
return 0;
}
double statistics(int *score,int num,int *maxId,int *minId)
{
int sum=0;
double average;
*maxId=score[0];
*minId=score[0];
for(int i=0;i<num;i++)
{
if(*maxId<=score[i])
*maxId=score[i];
}
for(int i=0;i<num;i++)
{
if(*minId>=score[i])
*minId=score[i];
}
for(int i=0;i<num;i++)
{
sum+=score[i];
}
average=sum/num;
return average;
}

• 写回答
• 好问题 提建议
• 追加酬金
• 关注问题
• 邀请回答

#### 1条回答默认 最新

• qzjhjxj 2022-05-15 00:46
最佳回答

修改如下，供参考：

``````#include <stdio.h>
#include <stdlib.h>
double statistics(int *score,int num,int *maxId,int *minId);
int main()
{
int i,score[200],num,maxId,minId;//修改
double average;
scanf("%d",&num);
FILE* fp;
fp=fopen("成绩.txt","r");
if(fp != NULL) {          //修改
for(i=0;i<num;i++)
if(fscanf(fp,"%d",&score[i]) != 1) break;//修改
fclose(fp);
average = statistics(score,i,&maxId,&minId);//修改
//statistics(&score[200],num,&maxId,&minId);
printf("%d %d %lf",maxId,minId,average);
}
else
printf("Open file fail!\n");//修改

return 0;
}
double statistics(int *score,int num,int *maxId,int *minId)
{
int sum=0;
double average=0;
*maxId=score[0];
*minId=score[0];
for(int i=0;i<num;i++) //修改
{
if(*maxId < score[i]) //修改
*maxId=score[i];
if(*minId > score[i]) //修改
*minId=score[i];
sum+=score[i];
}
if(num != 0) //修改
average=(double)sum/num; //修改
return average;
}

``````
评论
解决 1 无用
打赏 举报