您的位置:首页 > 编程语言 > C语言/C++

《C语言及程序设计》实践参考——成绩处理函数版

2015-04-29 17:10 260 查看
返回:贺老师课程教学链接 项目要求

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

,其中为样本(即某同学成绩),为均值(前面已经求出),为样本数目;
界面参考见图



  “成绩处理”是该功能不用函数的实现。本项目要求输入、求最大/小值等所有功能都通过自定义函数完成。这种设计貌似比不用函数的方法麻烦,但这只是对函数不熟悉时的感觉。而实际上,采用函数的做法,在结构有更多的优点,尤其是当程序的规模更大时。通过这个项目,学会将数组名用作函数的参数解决问题的方法。
  下面是建议的自定义函数的声明和main()函数,你需要定义这些函数。

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( )
{
    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;
}
[参考解答]

#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 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; }

/*input_score函数的功能是输入小组成员的成绩
*入口参数:
s - 存放成绩的数组
n - 学生人数
*返回值:无
*/
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]<0||s[i]>100);
return;
}

/* get_max_score函数的功能是求出n名同学成绩中的最高成绩
*入口参数:
s - 存放成绩的数组
n - 学生人数
*返回值:最高成绩
*/
int get_max_score(int s[], int n)
{
int max = -1;
int i;
for(i=0; i<n; i++)
if(max<s[i]) max= s[i];
return max;
}

/* get_min_score函数的功能是求出n名同学成绩中的最低成绩
*入口参数:
s - 存放成绩的数组
n - 学生人数
*返回值:最低成绩
*/
int get_min_score(int s[], int n)
{
int min = 999;
int i;
for(i=0; i<n; i++)
if(min>s[i]) min= s[i];
return min;
}

/*get_avg_score函数的功能是求出num名同学成绩中的平均成绩
*入口参数:
s - 存放成绩的数组
n - 学生人数
*返回值:平均成绩
*/
double get_avg_score(int s[], int n)
{
double sum = 0;
int i;
for(i=0; i<n; i++)
sum+=s[i];
return sum/n;
}

/* get_ stdev _score函数的功能是求出num名同学成绩的标准偏差
*入口参数:
s - 存放成绩的数组
n - 学生人数
*返回值:标准偏差
*/
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));
}

/* count函数的功能是返回值s数组中为m的元素的个数
*入口参数:
m - 要查找计数的值
s - 存放成绩的数组
n - 学生人数
*返回值:m出现的数目
*/
int count(int m, int s[], int n)
{
int iCount=0;
int i;
for(i=0; i<n; i++)
{
if(s[i]==m)
iCount++;
}
return iCount;
}

/*output_index函数的功能是输出s数组中值为m的元素的下标(index)
*注意:值为m的元素可能有多个
*入口参数:
m - 要查找的值
s - 存放成绩的数组
n - 学生人数
*返回值:无
*/
void output_index(int m, int s[], int n)
{
int i;
for(i=0; i<n; i++)
{
if(s[i]==m)
printf("%d ", i);
}
return;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: