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

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

有人可以耐心看下代碼錯誤嗎

#include <stdio.h>

int sum(int score[10])

{

? ? int sums,i;

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

? ? {

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

? ? }

? ? return sums;

}

int max(int score[10])

{

? ? int maxs=0,i;

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

? ? {

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

? ? ? ? {

? ? ? ? ? ? maxs=score[i];

? ? ? ? }

? ? }

? ? return maxs;

}

int min(int score[10])

{

? ? int mins=max(score),i;

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

? ? {

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

? ? ? ? {

? ? ? ? ? ? mins=score[i];

? ? ? ? }

? ? }

? ? return mins;

}

int ave(int score[10])

{

? ? int aves;

? ? aves=(sum(score))/10;

? ? return aves;

}

void a(int score[10])

{

? ? int i,j,flag;

? ? for(i=9;i>=0;i--)

? ? {

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

? ? ? ? {

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

? ? ? ? ? ? {

? ? ? ? ? ? ? ? flag=score[j];

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

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

? ? ? ? ? ? }

? ? ? ? }

? ? }

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

? ? {

? ? ? ? if (i!=9)

? ? ? ? {

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

? ? ? ? }

? ? ? ? else?

? ? ? ? {

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

? ? ? ? }

? ? }

}

int main()

{

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

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

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

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

? ? printf("平均分是%d\n",ave(score));

? ? a(score);

? ? return 0;

}


正在回答

2 回答

你的問題出在:沒有定義局部變量sums的值。

你應該將 int sums,i; 改為 int sums=0,i;

如果你不定義局部變量sums的值,其值在你輸出總分時已經儲存在內存中,當你再次使用sums,它的值默認不為0,那么它的值不確定,因此平均數輸出值發生錯誤。


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

Poplar_Y

你可以試著按我說的修改,感受一下問題。
2019-10-29 回復 有任何疑惑可以回復我~
#2

qq_慕用5232731 回復 Poplar_Y

平均數的計算過程中有sums么?求大佬解答
2020-02-07 回復 有任何疑惑可以回復我~

#include <stdio.h>


int sum(int score[10])


{


? ? int sums,i;


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


? ? {


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


? ? }


? ? return sums;


}


int max(int score[10])


{


? ? int maxs=0,i;


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


? ? {


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


? ? ? ? {


? ? ? ? ? ? maxs=score[i];


? ? ? ? }


? ? }


? ? return maxs;


}


int min(int score[10])


{


? ? int mins=max(score),i;


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


? ? {


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


? ? ? ? {


? ? ? ? ? ? mins=score[i];


? ? ? ? }


? ? }


? ? return mins;


}


int ave(int score[10])


{


? ? int aves;


? ? aves=(sum(score))/10;


? ? return aves;


}


void a(int score[10])


{


? ? int i,j,flag;


? ? for(i=8;i>=0;i--)


? ? {


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


? ? ? ? {


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


? ? ? ? ? ? {


? ? ? ? ? ? ? ? flag=score[j];


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


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


? ? ? ? ? ? }


? ? ? ? }


? ? }


? ? for(i=9;i>=0;i--)


? ? {


? ? ? ? if (i!=0)


? ? ? ? {


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


? ? ? ? }


? ? ? ? else?


? ? ? ? {


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


? ? ? ? }


? ? }


}


int main()


{


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


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


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


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


? ? printf("平均分是%d\n",ave(score));


? ? a(score);


? ? return 0;


}


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

舉報

0/150
提交
取消

有人可以耐心看下代碼錯誤嗎

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

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

幫助反饋 APP下載

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

公眾號

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