一生客旅
2018-07-16 16:19
#include <stdio.h>
int main()
{
int score[10]={67,98,75,63,82,79,81,91,66,84};
int sum=0;
int i,j;
int temp;
float average;
for(i=0;i<=9;i++)
{
for(j=9;j>i;j--)
{
if(score[j]>score[j-1])
{
temp=score[j];
score[j-1]=score[j];
score[j]=temp;
}
}
printf("%d ",score[i]);
sum+=score[i];
}
average=sum/10;
printf("考试的总分=%d\n",sum);
printf("考试的平均分=%f\n",average);
printf("考试的最高分=%d\n",score[0]);
printf("考试的最低分=%d\n",score[9]);
return 0;
}
运行结果:98 98 91 91 91 91 91 91 84 84 ...
怎么会把原来的数组数据给改了?
在第二个for语句中,交换score[j-1]和score[j]的值时,score[j-1]的值被覆盖了,应改为:temp=score[j-1];
比较粗心,非常感谢
C语言入门
928255 学习 · 21546 问题
相似问题