社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
* 程序头部的注释结束
#include <iostream>
#include <iostream>
#include<Cmath>
using namespace std; //在这个问题中,成绩和人数是核心数据,适合作为全局变量处理
int score[50]; //将score设为全局变量,在各个函数中可以直接使用
int num; //小组人数也设为全局变量
void input_score();
int get_max_score();
int get_min_score();
double get_avg_score();
double get_stdev_score();
int count(int);
void output_index(int);
int main(void)
{
int max_score, min_score;
cout << "小组共有多少名同学?";
cin >> num;
cout << endl << "请输入学生成绩:" << endl;
input_score(); //要求成绩在0-100之间
max_score = get_max_score();
cout << endl << "最高成绩为:" << max_score << ",共有 " << count(max_score ) << " 人。";
min_score = get_min_score();
cout << endl << "最低成绩为:" << min_score << ",共有 " << count(min_score ) << " 人。";
cout << endl << "平均成绩为:" << get_avg_score();
cout << endl << "标准偏差为:" << get_stdev_score();
cout << endl << "获最高成绩的学生(学号)有:";
output_index(max_score);
cout << endl << "获最低成绩的学生(学号)有:";
output_index(min_score);
cout << endl;
system("PAUSE");
return 0;
}
void input_score() // input_score函数提供给同学们参考//input_score函数的功能是输入小组成员的成绩
{
int i;
for(i = 1; i <= num; i++)
{
do
{
cout << "输入第 " << i << " 位同学的成绩:";
cin >> score[i];
}while(score[i] < 0 || score[i] > 100);
}
return;
}
int get_max_score() // get_max_score()函数的功能是求出num名同学的最高成绩
{
int max, i;
max = score[1];
for(i = 1; i <= num; i++)
{
if(max < score[i])
{
max = score[i];
}
}
return max;
}
int get_min_score() // get_min_score()函数的功能是求出num名同学的最低成绩
{
int min, i;
min = score[1];
for(i = 1; i <= num; i++)
{
if(min >= score[i])
min = score[i];
}
return min;
}
double get_avg_score() // get_avg_score()函数的功能是求出num名同学的平均成绩
{
int i, aver, sum = 0;
for(i = 1; i <= num; i++)
{
sum += score[i];
}
aver=sum/num;
return aver;
}
double get_stdev_score() // get_ stdev _score()函数的功能是求出num名同学成绩的标准偏差
{
int i, a, sum=0, s, aver;
aver = get_avg_score();
for(i = 1; i <=num; i++)
{
a = score[i] - aver;
sum = sum + (a * a);
}
s = sqrt(sum / (num - 1));
return s;
}
int count(int s) // count(int s)函数的功能是返回值score数组中为s的元素的个数
{
int i, n = 0;
for(i = 1; i <= num; i++)
{
if(s == score[i])
n = n + 1;
}
return n;
}
void output_index(int s) // output_index函数的功能是输出score数组中值为s的元素的下标(index) //注意:值为s的元素可能有多个
{
int i;
for(i = 1; i <= num; i++)
{
if(s == score[i])
{
cout << i << " ";
}
}
return ;
}
经验积累:
1.单步执行是最好的帮手,有多处错误,也要用单步执行一步步解决。
2.对程序的可读性要多加努力,不然会造成不必要的麻烦啊!
3.能熟练运用数组和自定义函数,不仅能解决复杂和繁琐,并且能提高程序的易读性。
上机感言:这次是比较顺利的完成了,并且在思路上没有障碍,还是应该在课下多读书,对课本的深入理解,是解决编程难的根本方法!
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!