第十三周 项目三:成绩处理函数版
2014-11-20 17:10
204 查看
问题及代码:
运行结果:
知识点总结:
略大的程序,用到了自定义函数,虽然代码行数很多,但是运行效率更高。
学习心得:
在上一篇的基础上,使用自定义函数,要注意的地方有很多,继续加油~!多文件弄不成功,待我继续学习多文件~~~
/* * Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:Project4.cpp * 作 者:李楠 * 完成日期:2014年11月20日 * 版 本 号:v1.0 * * 问题描述: 项目2的另一种实现。 * 输入描述: 一个整数 * 程序输出: 略 */ #include <iostream> #include <cmath> using namespace std; 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(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; for(i=0;i<n;++i) { cout<<"输入第"<<i<<"位同学的成绩:"; cin>>s[i]; if(s[i]>100||s[i]<0) i--; } return; } int get_max_score(int s[], int n)//最高成绩的输出 { int i,max=0; for(i=0;i<n;++i) { if(max<=s[i]) max=s[i]; } return max; } int get_min_score(int s[], int n)//最低成绩的输出 { int i,min=100; for(i=0;i<n;++i) { if(min>=s[i]) min=s[i]; } return min; } double get_avg_score(int s[], int n)//返回数组s中n名同学的平均成绩值 { int i,sum=0; double ave; for(i=0;i<n;++i) { sum+=s[i]; } ave=sum/n; return ave; } double get_stdev_score(int s[], int n)//返回数组s中n名同学成绩值的标准偏差 { double sdeviation; double psum=0; int p,i; for(i=0;i<n;++i) { p=s[i]-get_avg_score(s, n); psum+=p*p; } sdeviation=sqrt(psum/(n-1)); return sdeviation; } int count(int x, int s[], int n)//返回在数组s中n名同学中有多少人得x分 { int m=0,i; for(i=0;i<n;++i) { if(x==s[i]) m++; } return m; } void output_index(int x, int s[], int n)//在函数中输出数组s中n名同学中得x分的学号(下标) { int i; for(i=0;i<n;++i) { if(x==s[i]) cout<<i<<" "; } return; }
运行结果:
知识点总结:
略大的程序,用到了自定义函数,虽然代码行数很多,但是运行效率更高。
学习心得:
在上一篇的基础上,使用自定义函数,要注意的地方有很多,继续加油~!多文件弄不成功,待我继续学习多文件~~~
相关文章推荐