问题描述:
在数组score中将要存储了某小组C程序设计的成绩,请设计实现下面的各功能函数,并在main函数中调用,组合成一个完整的应用:
(1)输入小组人数及成绩,要保证成绩在0-100之间;
(2)输出该小组的最高成绩、最低成绩、平均成绩;
(3)输出考得最高成绩和最低成绩的同学的人数;
(4)输出考得最高成绩和最低成绩的同学的学号(设数组下标即学号,可能有相同的成绩)。
(5)求出所有同学成绩的标准偏差,标准偏差公式为,其中为样本(即某同学成绩),为均值(前面已经求出),为样本数目;
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
*csdn学院--2016级
*目的:让代码见证成长(作为一个初学的菜鸟,如
*大家有发现错误,欢迎指正!)
*文件名称:Myfun166.c
*作者:小臣小仁
*完成日期:2017年7月16日
*/
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 count1(int x, int s[], int n); //返回在数组s中n名同学中有多少人得x分(实参给出最高/低时,可以求最高/低成绩的人数)
void output_index(int x, int s[], int n); //在函数中输出数组s中n名同学中得x分的学号(下标)
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, count1(max_score, score, num ));
min_score=get_min_score(score, num);
printf("最低成绩为:%d,共有 %d 人\n", min_score, count1(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)//将小组中n名同学的成绩输入数组s
{
int i;
for(i=0;i<n;i++)
{
do{
printf("请输入第%d位同学的分数:",i);
scanf("%d",&s[i]);
}while(s[i]<0||s[i]>100);
}
}
int get_max_score(int s[], int n)//返回数组s中n名同学的最高成绩值
{
int i,max;
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)//返回数组s中n名同学的最低成绩值
{
int i,min;
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)//返回数组s中n名同学的平均成绩值
{
int i;
double ave,sun=0;
for(i=0;i<n;i++)
sun+=s[i];
return ave=sun/n;
}
double get_stdev_score(int s[], int n)//返回数组s中n名同学成绩值的标准偏差
{
int i;
double score;
double sum=0;
score=get_avg_score(s,n);
for(i=0;i<n;i++)
sum+=(s[i]-score)*(s[i]-score);
return sqrt(sum/(n-1));
}
int count1(int x, int s[], int n)//返回在数组s中n名同学中有多少人得x分(实参给出最高/低时,可以求最高/低成绩的人数)
{
int i=0,num=0;
while(i<n)
{
if(s[i]==x)
num++;
i++;
}
return num;
}
void output_index(int x, int s[], int n)//在函数中输出数组s中n名同学中得x分的学号(下标)
{
int i=0;
while(i<n)
{
if(x==s[i])
printf("%d\t",i);
i++;
}
}
输出结果:
心得体会:
作为一个大学二的菜鸟,开始慢慢前进,多多少少感到有收获,由于水平有限,多多少少有错,望包涵转存失败重新上传取消