第十三週項目3 成績統計函數版本

/*
*Copyright (c)2014,煙臺大學計算機與控制工程學院
*All rights reserved.
*文件名稱:test.cpp
*作    者:anGelovEr 王坤
*完成日期:2014年11月23日
*版 本 號:v1.0
*
*問題描述:成績統計(函數版)
*程序輸出:最高分最低分平均分標準差等。
*/
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
void input_score(int s[], int n); //將小組中n名同學的成績輸入數組s
int get_max_score(int s[], int n);  //返回數組s中n名同學的最高成績值
int get_min_score(int s[], int n);  //返回數組s中n名同學的最低成績值
double get_avg_score(int s[], int n);  //返回數組s中n名同學的平均成績值
double get_stdev_score(int s[], int n); //返回數組s中n名同學成績值的標準偏差
int count(int x, int s[], int n);  //返回在數組s中n名同學中有多少人得x分(實參給出最高/低時,可以求最高/低成績的人數)
void output_index(int x, int s[], int n); //在函數中輸出數組s中n名同學中得x分的學號(下標)
int main(void)
{
    int score[50]; //將score設爲局部變量,通過數組名作函數參數,傳遞數組首地址,在函數中操作數組
    int num;       //小組人數也設爲局部變量,將作爲函數的實際參數
    int max_score,min_score;
    cout<<"小組共有多少名同學?";
    cin>>num;
    cout<<endl<<"請輸入學生成績:"<<endl;
    input_score(score, num);  //要求成績在0-100之間
    max_score=get_max_score(score, num);
    cout<<endl<<"最高成績爲:"<<max_score<<",共有 "<<count(max_score, score, num )<<" 人。";
    min_score=get_min_score(score, num);
    cout<<endl<<"最低成績爲:"<<min_score<<",共有 "<<count(min_score,score, num )<<" 人。";
    cout<<endl<<"平均成績爲:"<<get_avg_score(score, num);
    cout<<endl<<"標準偏差爲:"<<get_stdev_score(score, num);
    cout<<endl<<"獲最高成績的學生(學號)有:";
    output_index(max_score,score, num);
    cout<<endl<<"獲最低成績的學生(學號)有:";
    output_index(min_score,score, num);
    cout<<endl;
    return 0;
}
void input_score(int s[], int n)  //將小組中n名同學的成績輸入數組s
{
    int i=1;
    while(i<=n) //輸入num名同學的成績
    {
        cout<<"請輸入第"<<i<<"位同學的成績:";
        cin>>s[i];
        if(s[i]>=0&&s[i]<=100)  //成績範圍爲0-100
            ++i;
        else
            cout<<"請檢查數據並重新輸入學生成績(0-100):"<<endl;  //輸入錯誤後要重新輸入
    }
}
int get_max_score(int s[], int n)  //返回數組s中n名同學的最高成績值
{
    int max,j;
    max=-1;
    for(j=1; j<=n; j++)
    {
        if(s[j]>max)
            max=s[j];
    }
    return (max);
}
int get_min_score(int s[], int n)  //返回數組s中n名同學的最低成績值
{
    int min,j;
    min=101;
    for(j=1; j<=n; j++)
    {
        if(s[j]<min)
            min=s[j];
    }
    return (min);
}
double get_avg_score(int s[], int n)  //返回數組s中n名同學的平均成績值
{
    int j;
    double sum=0;
    for(j=1; j<=n; j++)
        sum+=s[j];
    return (sum/n);
}
double get_stdev_score(int s[], int n)  //返回數組s中n名同學成績值的標準偏差
{
    double m=0;
    int x,y,j;
    for(j=1; j<=n; j++)
    {
        x=s[j]-(get_avg_score(s, n));
        y=x*x;
        m+=y;
    }
    return (sqrt(m/(n-1)));
}
int count(int x, int s[], int n)  //返回在數組s中n名同學中有多少人得x分(實參給出最高/低時,可以求最高/低成績的人數)
{
    int j,count=0;
    for(j=1; j<=n; j++) //求出並輸出考得最高成績和最低成績人數
    {
        if(s[j]==x)
            count++;
    }
    return (count);
}
void output_index(int x, int s[], int n)  //在函數中輸出數組s中n名同學中得x分的學號(下標)
{
    int j;
    for(j=1; j<=n; j++)  //再次篩選出考得最高成績和最低成績的學號
    {
        if(s[j]==x)
            cout<<j<<" ";
    }
}

知識點總結:太長了……

學習心得:太費事了……果然需要team啊

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