C++第14周项目3——成绩处理第二季

简介: 课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565【项目3 - 成绩处理第二季】项目2的另一种实现。其中用于存储学生成绩的数组和学生人数的变量均是main()函数的局部变量。这种设计貌似比项目2麻烦,但其结构有更多的优点,尤其是当这个程序的规模更大时。通过这个项目,学会将数组名用作函数的参数。  下面给出main()

课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565

【项目3 - 成绩处理第二季】项目2的另一种实现。其中用于存储学生成绩的数组和学生人数的变量均是main()函数的局部变量。这种设计貌似比项目2麻烦,但其结构有更多的优点,尤其是当这个程序的规模更大时。通过这个项目,学会将数组名用作函数的参数。

  下面给出main()函数,在此之前,只允许声明函数和#include<...>,不允许定义数据。
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);    
  return 0; 
}

参考解答:
#include <iostream>
#include<Cmath>
using namespace std;
//在这个问题中,成绩和人数是核心数据,适合作为全局变量处理
void input_score(int[], int);
int get_max_score(int[], int);
int get_min_score(int[], int);
double get_avg_score(int[], int);
double get_stdev_score(int[], int);
int count(int, int[], int);
void output_index(int, int[], int);

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

/*input_score函数的功能是输入小组成员的成绩
 *入口参数:
     s - 存放成绩的数组
	 n - 学生人数
 *返回值:无
 */
void input_score(int s[], int n)
{
    int i;
    for(i=0;i<n;i++)
		do
		{
			cout<<"输入第"<<i<<" 位同学的成绩:";
			cin>>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)
			cout<<i<<" ";
	}
	return;
}



目录
相关文章
|
29天前
|
开发工具 C语言 C++
CMake构建大型C/C++项目:跨平台设计与高级应用(二)
CMake构建大型C/C++项目:跨平台设计与高级应用
41 0
|
30天前
|
设计模式 测试技术 编译器
C++项目中打破循环依赖的锁链:实用方法大全(一)
C++项目中打破循环依赖的锁链:实用方法大全
78 0
|
3月前
|
存储 机器学习/深度学习 算法
使用 OpenCV4 和 C++ 构建计算机视觉项目:1~5
使用 OpenCV4 和 C++ 构建计算机视觉项目:1~5
|
8天前
|
存储 算法 Linux
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
25 6
|
17天前
C/C++test两步完成CMake项目静态分析
通过将C/C++test集成到CMake项目中,并根据项目的需要进行配置,可以在两步内完成CMake项目的静态分析。这样可以帮助开发人员及时发现并修复潜在的代码问题,提高代码质量和可靠性。
8 0
|
24天前
|
IDE 算法 编译器
快速掌握陌生C++项目的科学与心理学策略
快速掌握陌生C++项目的科学与心理学策略
56 0
|
24天前
|
敏捷开发 安全 API
C/C++ 工程师面试:如何精彩展示你的项目经验并获得高分
C/C++ 工程师面试:如何精彩展示你的项目经验并获得高分
71 0
|
29天前
|
消息中间件 存储 算法
【C/C++ 泡沫精选面试题04】在实际项目中,多进程和多线程如何选择?
【C/C++ 泡沫精选面试题04】在实际项目中,多进程和多线程如何选择?
43 1
|
29天前
|
编译器 持续交付 项目管理
CMake构建大型C/C++项目:跨平台设计与高级应用(三)
CMake构建大型C/C++项目:跨平台设计与高级应用
37 0
|
29天前
|
编译器 Linux C语言
CMake构建大型C/C++项目:跨平台设计与高级应用(一)
CMake构建大型C/C++项目:跨平台设计与高级应用
66 0