• 第十三周项目3-成绩处理函数版


    成绩处理函数版

    在数组score中将要存储某小组C++程序设计的成绩,请设计实现下面的各功能函数,并在main函数中调用,组合成一个完整的应用:
    (1)输入小组人数及成绩,要保证成绩在0-100之间;
    (2)输出该小组的最高成绩、最低成绩、平均成绩;
    (3)输出考得最高成绩和最低成绩的同学的人数;
    (4)输出考得最高成绩和最低成绩的同学的学号(设数组下标即学号,可能有相同的成绩)。
    (5)求出所有同学成绩的标准偏差,标准偏差公式为,其中为xi样本(即某同学成绩),x(上带一横)为均值(前面已经求出),N为样本数目;

    运用以下函数:

    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分的学号(下标)


    运行代码:

    /*
     *Copyright (c) 2014,烟台大学计算机学院
     *All gight reserved.
     *文件名称:temp.cpp
     *作者:邵帅
     *完成时间:2014年11月20日
     *版本号:v1.0
    */
    #include<iostream>
    #include<cmath>
    using namespace std;
    void input_score(int s[], int n); 
    int get_max_score(int s[], int n);  
    int get_min_score(int s[], int n);  
    double get_avg_score(int s[], int n);  
    double get_stdev_score(int s[], int n); 
    int count(int x, int s[], int n);  
    void output_index(int x, int s[], int n); 
    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)
    {
        int i=0;
        while (i<n)
        {
            cout<<"第"<<i<<"名同学的成绩:";
            cin>>s[i];
            if (s[i]<0 || s[i]>100)
            {
                cout<<"成绩输入错误,请重新输入。"<<endl;
                continue;
            }
            i++;
        }
    }
    int get_max_score(int s[], int n)
    {
        int i,max=s[0];
        for (i=0; i<n; i++)
        {
            if (s[i]>max)
                max=s[i];
        }
        return max;
    }
    int get_min_score(int s[], int n)
    {
        int i,min=s[0];
        for (i=0; i<n; i++)
        {
            if (s[i]<min)
                min=s[i];
        }
        return min;
    }
    double get_avg_score(int s[], int n)
    {
        int sum=0,ave;
        for (int i=0; i<n; i++)
        {
            sum=sum+s[i];
        }
        ave=sum/n;
        return ave;
    }
    double get_stdev_score(int s[], int n)
    {
        double SUM=0,NUM=0;
        for (int i=0; i<n; i++)
        {
            SUM=(s[i]-get_avg_score(s, n))*(s[i]-get_avg_score(s, n));
            NUM=NUM+SUM;
        }
        NUM=sqrt(NUM/(n-1));
        return NUM;
    }
    int count(int x, int s[], int n)
    {
        int i,j=0;
        for (i=0; i<n; i++)
        {
            if (s[i]==x)
                j++;
        }
        return j;
    }
    void output_index(int x, int s[], int n)
    {
        int i,m=0,j=0,a[50];
        for (i=0; i<n; i++)
        {
            if (s[i]==x)
            {
                m++;
                a[j]=i;
                j++;
            }
        }
        for (j=0; j<m; j++)
            cout<<a[j]<<" ";
    }


    运行结果:

    @ Mayuko

  • 相关阅读:
    Anytime Stereo Image Depth Estimation on Mobile Devices论文阅读笔记
    PSPNet论文阅读笔记
    阅读笔记Underexposed Photo Enhancement using Deep Illumination Estimation
    论文阅读笔记之Replacing Mobile Camera ISP with a Single Deep Learning Model
    Learning to See in the Dark论文阅读笔记
    机器安装不同版本的cuda
    Cycle-GAN论文阅读笔记
    EnlightenGAN: Deep Light Enhancement without Paired Supervision论文阅读笔记
    Homography Based Egomotion Estimation with a Common Direction论文解读
    homography分解为R和t的matlab实现
  • 原文地址:https://www.cnblogs.com/mayuko/p/4567614.html
Copyright © 2020-2023  润新知