、編程實現輸入8個學生某門功課的成績,用函數求出平均分,并在主函數中輸出平均分以及高于平均分的同學的
、編程實現輸入8個學生某門功課的成績,用函數求出平均分,并在主函數中輸出平均分以及高于平均分的同學的
代碼如下:#include <;stdio.h>。float getAvg(float score[]){。float result=0。int i。for(i=0;i<;8;i++){。result = result + score[i]。};result = result/8。return result。};void main(){。float score[8];/*保存8個學生成績*/。float avg=0;/*平均成績*/。int i。for(i=0;i<;8;i++){。printf(";\;n請輸入第%d學生的成績 : ";,i)。scanf(";%f";,&score[i])。};avg = getAvg(score)。
導讀代碼如下:#include <;stdio.h>。float getAvg(float score[]){。float result=0。int i。for(i=0;i<;8;i++){。result = result + score[i]。};result = result/8。return result。};void main(){。float score[8];/*保存8個學生成績*/。float avg=0;/*平均成績*/。int i。for(i=0;i<;8;i++){。printf(";\;n請輸入第%d學生的成績 : ";,i)。scanf(";%f";,&score[i])。};avg = getAvg(score)。
編程實現輸入8個學生某門功課的成績,用函數求出平均分,并在主函數中輸出平均分以及高于平均分的同學的成績。以下是實現代碼:代碼如下:#include
float getAvg(float score[]){float result=0;int i;for(i=0;i<8;i++){result = result + score[i];}result = result/8;return result;}void main(){float score[8];/*保存8個學生成績*/float avg=0;/*平均成績*/int i;for(i=0;i<8;i++){printf("\n請輸入第%d學生的成績 : ",i);scanf("%f",&score[i]);} avg = getAvg(score);printf("\n這8個學生的平均成績是:%.2f\n",avg);printf("高于平均成績的分數:\n");for(i=0;i<8;i++)if(score[i]>avg)printf("%.2f,",score[i]);}在主函數中,首先定義了一個數組score來保存8個學生的成績。然后通過循環輸入每個學生的成績。接著調用getAvg函數計算平均分,并將結果存儲在變量avg中。最后,再次遍歷數組score,輸出高于平均分的成績。該程序的主要功能是通過函數實現成績的計算和篩選,同時通過控制臺與用戶交互,接收輸入并展示結果。這種設計使得程序結構清晰,易于維護。代碼中的細節包括使用了格式化輸入輸出,確保數據的準確性和可讀性。同時,通過函數分離了計算和顯示邏輯,提高了代碼的可重用性和可讀性。以上代碼適用于C語言環境,通過簡單的修改可以應用于其他編程語言中。希望這個示例能夠幫助你理解如何通過編程解決實際問題。
、編程實現輸入8個學生某門功課的成績,用函數求出平均分,并在主函數中輸出平均分以及高于平均分的同學的
代碼如下:#include <;stdio.h>。float getAvg(float score[]){。float result=0。int i。for(i=0;i<;8;i++){。result = result + score[i]。};result = result/8。return result。};void main(){。float score[8];/*保存8個學生成績*/。float avg=0;/*平均成績*/。int i。for(i=0;i<;8;i++){。printf(";\;n請輸入第%d學生的成績 : ";,i)。scanf(";%f";,&score[i])。};avg = getAvg(score)。
為你推薦