/*在一個長度為10的整型數(shù)組里面,保存了班級10個學(xué)生的考試成績。要求編寫5個函數(shù),分別實現(xiàn)計算考試的總分,最高分,最低分,平均分和考試成績降序排序。*/#include<stdio.h>//總分int addCount(int score[])//注意函數(shù)中數(shù)組如何作為形參{ int i,sum=0;//要給sum賦初值 for(i=0;i<10;i++) { sum +=score[i]; } return sum;}//平均成績float avgCount(int score[]){ float avg; avg = float(addCount(score)/10); return avg;}//最高分int maxCount(int score[]){ int i; int max = score[0];//比較大小的時候要引入一個中間變量,作為媒介 for(i=0;i<10;i++) { if(max<score[i]) { max = score[i]; } } return max;}//最低分int minCount(int score[]){ int i; int min = score[0]; for(i=0;i<10;i++) { if(min>score[i]) { min = score[i]; } } return min;}//考試成績降序int sort(int score[]){ int i,j,mid; for(i=0;i<10;i++) { for(j=0;j<10;j++) { if(score[j]<score[j+1]) { mid = score[j]; score[j] = score[j+1]; score[j+1] = mid; } } } printf("排序的序列為:"); for(i=0;i<10;i++) { printf("%d\t",score[i]); } return 0;}//主函數(shù)int main(){? ? int score[10]={67,98,75,63,82,79,81,91,66,84}; printf("考試的總分為:%d\n",addCount(score)); printf("考試的平均分為:%lf\n",avgCount(score)); printf("考試的最高分為:%d\n",maxCount(score)); printf("考試的最低分為:%d\n",minCount(score)); printf("考試成績降序排序為:",sort(score));? ? return 0;}//分?jǐn)?shù)降序排序?void sort(int score[]){ int i,j; for(i=N-2;i>=0;i--) { for(j=0;j<=i;j++) { if(score[j]<score[j+1]) { int temp; temp = score[j]; score[j] = score[j+1];? score[j+1]=temp; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ?? } ? ? ? ? ? ? ? ? ?? } printScore(score); ? ??}?
添加回答
舉報
0/150
提交
取消