平均分一栏 网页输出正确,cmd输出 420183
#include <stdio.h> int main() { int score[]={67,98,75,63,82,79,81,91,66,84}; int a,i,j,max=0,min=0,ave; for(i=1;i<=8;i++) { if(score[i]>score[max]) { max=i; } } printf("本次考试最高分:%d\n",score[max]); for(i=1;i<=8;i++) { if(score[i]<score[min]) { min=i; } } printf("本次考试最低分:%d\n",score[min]); for(i=0;i<10;i++) { a+=score[i]; ave=a/(sizeof(score)/sizeof(score[0])); } printf("平均分:%d\n",ave); for(j=8;j>=0;j--) { for(i=0;i<=j;i++) { if(score[i]<score[i+1]) { int temp; temp=score[i]; score[i]=score[i+1]; score[i+1]=temp; } } } printf("本次考试成绩降序排序:\n"); for(j=0;j<10;j++) { printf("%d ",score[j]); } return 0; }
line 6: i<=9,同理,line 14:i<=9
求平均分的程序是错的,求平均分难道不应该是拿总分除以个数吗?
最后,附上答案
#include <stdio.h>
#define N 10
int sumScore(int score[N])
{
int i;
int sum=0;
for(i=0;i<N;i++)
{
sum+=score[i];
}
return sum;
}
int maxScore(int score[N])
{
int max = score[0];
int i;
for(i=1;i<N;i++)
{
if(max<score[i])max=score[i];
}
return max;
}
int minScore(int score[N])
{
int min = score[0];
int i;
for(i=1;i<N;i++)
{
if(min>score[i])min=score[i];
}
return min;
}
double avgScore(int score[N])
{
return (double)sumScore(score)/N;
}
void sort(int score[N])
{
int i,j;
for(i=N-1;i>0;i--)
{
for(j=0;j<i;j++)
{
int temp;
if(score[j]<score[j+1]){
temp = score[j];
score[j]=score[j+1];
score[j+1]=temp;
}
}
}
}
int main()
{
int score[N]={67,98,75,63,82,79,81,91,66,84};
int sum = sumScore(score);
int max = maxScore(score);
int min = minScore(score);
double avg = avgScore(score);
int i;
printf("sum=%d\n",sum);
printf("max=%d\n",max);
printf("min=%d\n",min);
printf("avg=%.2lf\n",avg);
sort(score);
printf("after sort, score[%d]={",N);
for(i=0;i<N;i++)
{
printf(" %d,",score[i]);
}
printf("\b}\n");
return 0;
}