/*******************************************************
Copyright (c) 2016,烟台大学计算机学院
All rights reserved.
作者:张明明
完成日期:2016年12月30日
版本号:v1.0
功能描述:输入n个学生成绩,算出其中最高分,最低分,平
均分,标准偏差,并求出最高分,最低分同学的个数和学号;
输入参数:num-学生个数
s-储存成绩的数组
返回参数:get_max_score-学生成绩中最高分
get_max_score—学生成绩中最低分
get_avg_score-学生的平均成绩值
get_stdev_score-学生成绩的标准的偏差
count-学生成绩的最高分或最低分的人数
output_index-输出学生成绩最高分和最低分的学号
其他说明:无
*******************************************************/
#include <stdio.h>
#include <stdlib.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分的学号(下标)
double avg;
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)
{
int i;
for(i=0;i<n;i++)
{
do
{
printf("第%d位学生的成绩为:",i);
scanf("%d",&s[i]);
}while(s[i]>100||s[i]<0);
}
return ;
}
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;
}
int count(int x, int s[], int n)
{
int i,sum=0;
for(i=0;i<n;i++)
{
if(s[i]==x)
sum++;
}
return sum;
}
double get_avg_score(int s[], int n)
{
int i,sum=0;
for(i=0;i<n;i++)
sum+=s[i];
avg=sum/n;
return avg;
}
double get_stdev_score(int s[], int n)
{
int i,sum=0;
double s1=0;
for(i=0;i<n;i++)
s1+=((s[i]-avg)*(s[i]-avg));
return sqrt(s1/(n-1));
}
void output_index(int x, int s[], int n)
{
int i=0;
while(s[i]!='\0')
{
if(s[i]==x)
printf("%d\t",i);
i++;
}
return 0;
}
第十八周学生成绩处理
最新推荐文章于 2022-01-23 09:11:14 发布