亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為何最后輸出排序的時候有個大數字的垃圾數字?


#include <stdio.h>

#define N 10

int scoreSum(int score[])

{

? ? int i;

? ? int sum = 0;

? ? for(i=0;i<N;i++)

? ? {

? ? ? ? sum += score[i];

? ? }

? ? return sum;?

}


int scoreMax(int score[])

{

? ? int i,max;

? ? for(i=0;i<N;i++)

? ? {

? ? ? ? max = score[0];

? ? ? ? if(score[i]>max)

? ? ? ? max = score[i];

? ? }

? ? return max;

? ??

}


int scoreMin(int score[])

{

? ? int i,min;

? ? for(i=0;i<N;i++)

? ? {

? ? ? ? min = score[0];

? ? ? ? if(score[i]<min)

? ? ? ? min = score[i];

? ? }

? ? return min;

}


int scoreAvg(int score[])

{

? ? int i;

? ? int sum = 0;

int avg = 0;

? ? for(i=0;i<N;i++)

? ? {

? ? ? ? sum += score[i];

? ? }

? ? avg = sum/N;

? ? return avg;?

}


int bubbleSort(int score[])

{

? ? int i,j;

? ? int temp=0;

? ? for(i=0;i<N;i++)

? ? {

? ? ? ? for(j=0;j<N;j++)

? ? ? ? {

? ? ? ? ? ? if(score[j]<score[j+1])

? ? ? ? ? ? {

? ? ? ? ? ? ? ? temp = score[j];

? ? ? ? ? ? ? ? score[j] = score[j+1];

? ? ? ? ? ? ? ? score[j+1] = temp;

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? for(i=0;i<10;i++)

? ? {

? ? ? ? printf("%d ",score[i]);

? ? }

return 0;

}


int main()

{

? ? int score[N]={67,98,75,63,82,79,81,91,66,84};

? ? int sum,avg,max,min;

sum = scoreSum(score);

avg = scoreAvg(score);

max = scoreMax(score);

min = scoreMin(score);

printf("總分是:%d\n",sum);

printf("平均分是:%d\n",avg);

printf("最高分是:%d\n",max);

printf("最低分是:%d\n",min);

? ? bubbleSort(score);

? ? return 0;

}


正在回答

1 回答

score[j]<score[j+1];當j=9,時沒有定義score[10],而且最大最小輸出不對。

#include <stdio.h>

#define N 10

int scoreSum(int score[])

{

? ? int i;

? ? int sum = 0;

? ? for(i=0;i<N;i++)

? ? {

? ? ? ? sum += score[i];

? ? }

? ? return sum;?

}


int scoreMax(int score[])

{

? ? int i,max;

? ? ?max = score[0];

? ? for(i=0;i<N;i++)

? ? {

? ? ? ? if(score[i]>max)

? ? ? ? max = score[i];

? ? }

? ? return max;

? ??

}


int scoreMin(int score[])

{

? ? int i,min;

? ? ?min = score[0];

? ? for(i=0;i<N;i++)

? ? {

? ? ? ? if(score[i]<min)

? ? ? ? min = score[i];

? ? }

? ? return min;

}


int scoreAvg(int score[])

{

? ? int i;

? ? int sum = 0;

int avg = 0;

? ? for(i=0;i<N;i++)

? ? {

? ? ? ? sum += score[i];

? ? }

? ? avg = sum/N;

? ? return avg;?

}


int bubbleSort(int score[])

{

? ? int i,j;

? ? int temp=0;

? ? ? ? for(j=0;j<N-1;j++)

? ? ? ? {

? ? ? ? ? ? if(score[j]<score[j+1])

? ? ? ? ? ? {

? ? ? ? ? ? ? ? temp = score[j];

? ? ? ? ? ? ? ? score[j] = score[j+1];

? ? ? ? ? ? ? ? score[j+1] = temp;

? ? ? ? ? ? }

? ? ? ? }

? ? for(i=0;i<10;i++)

? ? {

? ? ? ? printf("%d ",score[i]);

? ? }

return 0;

}


int main()

{

? ? int score[N]={67,98,75,63,82,79,81,91,66,84};

? ? int sum,avg,max,min;

sum = scoreSum(score);

avg = scoreAvg(score);

max = scoreMax(score);

min = scoreMin(score);

printf("總分是:%d\n",sum);

printf("平均分是:%d\n",avg);

printf("最高分是:%d\n",max);

printf("最低分是:%d\n",min);

? ? bubbleSort(score);

? ? return 0;

}


0 回復 有任何疑惑可以回復我~
#1

向上爬的魚 提問者

非常感謝!
2016-03-07 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為何最后輸出排序的時候有個大數字的垃圾數字?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號