第十四周课后作业——项目二



问题描述:在数组score中将要存储了某小组C程序设计的成绩,请设计实现下面的各功能函数,并在main函数中调用,组合成一个完整的应用:
 (1)输入小组人数及成绩,要保证成绩在0-100之间;
 (2)输出该小组的最高成绩、最低成绩、平均成绩;
 (3)输出考得最高成绩和最低成绩的同学的人数;
 (4)输出考得最高成绩和最低成绩的同学的学号(设数组下标即学号,可能有相同的成绩)。
 (5)求出所有同学成绩的标准偏差,标准偏差公式为这里写图片描述,其中 xi 为样本(即某同学成绩),这里写图片描述为均值(前面已经求出),N为样本数目;
 文件名称:_the_forteenth_week_homework_project_second
 作者:何知令
 发表时间:2016年12月6日
 输入:参考题目
 输出:参考题目

代码:

/*问题描述:在数组score中将要存储了某小组C程序设计的成绩,请设计实现下面的各功能函数,并在main函数中调用,组合成一个完整的应用: 
 (1)输入小组人数及成绩,要保证成绩在0-100之间; 
 (2)输出该小组的最高成绩、最低成绩、平均成绩; 
 (3)输出考得最高成绩和最低成绩的同学的人数; 
 (4)输出考得最高成绩和最低成绩的同学的学号(设数组下标即学号,可能有相同的成绩)。 
 (5)求出所有同学成绩的标准偏差,标准偏差公式为这里写图片描述,其中 xi 为样本(即某同学成绩),这里写图片描述为均值(前面已经求出),N为样本数目;
 文件名称:_the_forteenth_week_homework_project_second
 作者:何知令
 发表时间:2016年12月6日
 输入:参考题目
 输出:参考题目 */
#include <stdio.h>
#include <math.h>
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 i;
int main( )
{
    int score[50]; //将score设为局部变量,通过数组名作函数参数,传递数组首地址,在函数中操作数组
    int num;       //小组人数也设为局部变量,将作为函数的实际参数
    int max_score,min_score;
    printf("小组共有多少名同学? ");
    scanf("%d", &num);
    printf("请输入学生成绩:\n");
    input_score(score, num);  //要求成绩在0-100之间
    max_score=get_max_score(score, num);
    printf("最高成绩为:%d,共有 %d 人\n", max_score, count(max_score, score, num ));
    min_score=get_min_score(score, num);
    printf("最低成绩为:%d,共有 %d 人\n", min_score, count(min_score,score, num ));
    printf("平均成绩为:%.2f\n", get_avg_score(score, num));
    printf("标准偏差为:%.2f\n",get_stdev_score(score, num));
    printf("获最高成绩的学生(学号)有:");
    output_index(max_score,score, num);
    printf("\n获最低成绩的学生(学号)有:");
    output_index(min_score,score, num);
    printf("\n");
    return 0;
}
void input_score(int s[], int n)
{
    for(i=0; i<n; i++)
    {
        printf("输入第%d位同学的成绩:",i);
        scanf("%d",&s[i]);
        if(s[i]>100)
        {
            printf("输入第%d位同学的成绩:",i);
            scanf("%d",&s[i]);
        }
    }
}
int get_max_score(int s[], int n)
{
    int max_score=0;
    for(i=0; i<n; i++)
    {
        if(s[i]>max_score)
        {
            max_score=s[i];
        }
    }
    return max_score;
}
int get_min_score(int s[], int n)
{
    int min_score=100;
    for(i=0; i<n; i++)
    {
        if(s[i]<min_score)
        {
            min_score=s[i];
        }
    }
    return min_score;
}
double get_avg_score(int s[], int n)
{
    double count=0,aver;
    for(i=0; i<n; i++)
    {
        count=count+s[i];
    }
    aver=count/n;
    return aver;
}
double get_stdev_score(int s[], int n)
{
    double sum = 0,mean_score, x;
    int i;
    mean_score =get_avg_score(s,n);
    for(i=0; i<n; i++)
    {
        x=s[i]-mean_score;
        sum+=x*x;
    }
    return sqrt(sum/(n-1));
}
int count(int x, int s[], int n)
{
    int k=0;
    for(i=0; i<n; i++)
    {
        if(s[i]==x)
        {
            k++;
        }
    }
    return k;
}
void output_index(int x, int s[], int n)
{
    for(i=0; i<n; i++)
    {
        if(s[i]==x)
        {
            printf("%d ",i);
        }
    }
}

程序运行结果展示:

知识点总结:函数调用,数组

学习心得:被标准偏差卡死,出都出不来,不得以,抄....(仅那个函数)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值