#include
#define N 10
int main()
{
float score[N],Maxscore=0,Minscore=0,sum=0,ave;
int Num[10],Maxnum,Minnum,i;
for(i=0;i
{
printf("請輸入第%d個學生的學號和成績:\n",i+1);
scanf("%d,%f",&Num[i],&score[i]);
sum=sum+Num[i];
if(score[i]>Maxscore)
{
Maxscore=score[i];
Maxnum=Num[i];
}
else if(score[i]<Minscore)
{
Minscore=score[i];
Maxnum=Num[i];
}
}
ave=sum/10;
printf("平均分為%d,最高分及其學分為%f,%d,最低分及其學分為%f,%d\n",ave,Maxscore,Maxnum,Minscore,Minnum);
return 0;
}
題目是依次輸入十個學生的成績和學號,求平均分和最高分和對應的學號以及最低分和對應的學號。我這為啥會這樣……
好幾個錯誤:
1.Minscore應該設一個很大的初始值,或者i==0的時候將這個值賦值給Minscore
2.sum=sum+Num[i];應該是sum = sum + score[i];
3.else if(score[i]<Minscore)裡Minnum = Num[i];寫成了Maxnum=Num[i];
4.printf("平均分為%d,最高分及其學分為%f,%d,最低分及其學分為%f,%d\n",ave,Maxscore,Maxnum,Minscore,Minnum);應該是printf("平均分為%f,最高分及其學分為%f,%d,最低分及其學分為%f,%d\n",ave,Maxscore,Maxnum,Minscore,Minnum);
#include<stdio.h>
#define N 10
int main()
{
float score[N], Maxscore = 0, Minscore = 9999999, sum = 0, ave;
int Num[N], Maxnum, Minnum, i;
for (i = 0; i<N; i++)
{
printf("請輸入第%d個學生的學號和成績:\n", i + 1);
scanf("%d,%f", &Num[i], &score[i]);
sum = sum + score[i];
if (score[i]>Maxscore)
{
Maxscore = score[i];
Maxnum = Num[i];
}
else if (score[i]<Minscore)
{
Minscore = score[i];
Minnum = Num[i];
}
}
ave = sum / N;
printf("平均分為%f,最高分及其學分為%f,%d,最低分及其學分為%f,%d\n", ave, Maxscore, Maxnum, Minscore, Minnum);
return 0;
}