哪位大佬幫忙看看哪里錯(cuò)了
#include <stdio.h>
#define N 10
int totalscore(int score[])
{
? ? for(int i=0;i<10;i++)
? ? {
? ? ? ? int total=0;
? ? ? ? total=total+score[i];
? ? }
? ? return total;
}
int maxscore(int score[])
{
? ? int max=score[0];
? ? for(int i=0;i<9;i++)
? ? {
? ? ? ? if(score[i]>max)
? ? ? ? {
? ? ? ? ? ? max=score[i];
? ? ? ? }
? ? }
? ? return max;
}
int minscore(int score[])
{
? ? int min=score[0];
? ? for(int i=0;i<9;i++)
? ? {
? ? ? ? if(score[i]<min)
? ? ? ? {
? ? ? ? ? ? min=score[i];
? ? ? ? }
? ? }
? ? return min;
}
int averagescore(int score[])
{
? ? int average;
? ? average=(totalscore(score))/10;
? ? return average;
}
void orderscore(int score[],N)
{
? ? for(int i=N-1;i>=0;i--)
? ? {
? ? ? ? for(int j=0;j<=i;j++)
? ? ? ? {
? ? ? ? ? ? if(score[j]<score[j+1])
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int temp=score[j];
? ? ? ? ? ? ? ? score[j]=score[j+1];
? ? ? ? ? ? ? ? score[j+1]=score[j];
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? for(int i=0;i<10;i++)
? ? {
? ? printf("成績降序排列為:",score[i]);
? ? ? ??
? ? }
}
int main()
{
? ??
? ? int score[N]={67,98,75,63,82,79,81,91,66,84};
? ? int Total=totalscore(score);
? ? int Max=maxscore(score);
? ? int Min=minscore(score);
? ? int Average=averagescore(score);
? ??
? ? printf("總分為:%d",Total);
? ? printf("最高分為:%d",Max);
? ? printf("最低分為:%d",Min);
? ? printf("平均分為:%d",Average);
? ? order(score);
? ? return 0;
}
2021-08-17
主要問題在排序函數(shù)
1,首先調(diào)用的函數(shù)名order(score);和您定義的函數(shù)名void orderscore(int score[],N)就匹配不上,
2,您的冒泡排序算法寫錯(cuò)了,應(yīng)該是數(shù)組越界了,
第一層循環(huán),i取值范圍9->0,
第二層循環(huán),j取值范圍0->9,這里就出問題了,j+1的取值范圍就是1->10,當(dāng)score[10]就是越界了
3,可以把j的取值范圍限制到0->8,就沒問題了,這段修改一下
?for(int i=N-1;i>=0;i--)
? ? {
? ? ? ? for(int j=0;j<=i-1;j++)
? ? ? ? {
? ? ? ? ? ? if(score[j]<score[j+1])
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int temp=score[j];
? ? ? ? ? ? ? ? score[j]=score[j+1];
? ? ? ? ? ? ? ? score[j+1]=score[j];
? ? ? ? ? ? }
? ? ? ? }
? ? }