第十三週項目一學生成績處理

題目描述:編寫一個函數void calcscore(int n),在函數中輸入n個人的成績,計算最高分,最低分,總分和平均分,要求在主函數中調用函數calcscore計算各種成績,並在主函數中輸出各種計算結果。(使用全局變量在函數之間傳遞多個數據)

作者:李忠林

時間:2016.11.28

代碼:

#include <stdio.h>
double HighScore; /*全局變量,最高分*/
double LowScore; /*全局變量,最低分*/
double SumScore; /*全局變量,總分*/
double AverageScore; /*全局變量,平均分*/
void calcscore(int n); /*函數聲明*/
int main()
{
    int n;
    scanf("%d",&n);
    calcscore(n);
    printf("%g %g %g %g\n",HighScore,LowScore,SumScore,AverageScore);
    return 0;
}
void calcscore(int n)
{
    int i;
    double s;
    HighScore=-1;
    LowScore=100;
    AverageScore=0;
    for(i=0; i<n; i++)
    {
        scanf("%lf",&s);
        if(s>HighScore)
        {
            HighScore=s;
        }
        if(s<LowScore)
        {
            LowScore=s;
        }
        SumScore+=s;
    }
    AverageScore=SumScore/n;
    return;
}



運行結果:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章