现在的位置: 首页 > 综合 > 正文

第十四周实验报告二(用循环控制语句编写程序 学生人数和成绩)

2013年01月17日 ⁄ 综合 ⁄ 共 2320字 ⁄ 字号 评论关闭

 第14周报告2:
实验目的:学会使用循环控制语句解决实际问题
实验内容:用循环控制语句编写程序,完成表达式的计算

* 程序头部注释开始(为避免提交博文中遇到的问题,将用于表明注释的斜杠删除了)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:                             
* 作    者:  冯珍珍                           
* 完成日期:    2011     年  11     月      23  日
* 版本号:         

* 对任务及求解方法的描述部分
* 输入描述:学生人数和成绩
* 问题描述:(函数及数组的简单应用) 在数组score中将要存储某小组C++程序设计的成绩,请设计完成下面功能函数,并将它们组合成一个完整的应用:
(1)输入小组人数及成绩;
(2)输出该小组的最高成绩、最低成绩、平均成绩和成绩的标准偏差;
(3)输出考得最高成绩和最低成绩的同学的人数及对应的学号(设成绩对应的下标即学号,可能有相同的成绩)
* 程序输出:小组的最高成绩、最低成绩、平均成绩和成绩的标准偏差;最高成绩和最低成绩的同学的人数及对应的学号
* 问题分析:……
* 算法设计:……
* 程序头部的注释结束(此处也删除了斜杠)
#include <iostream>
#include<Cmath>
using namespace std;
//在这个问题中,成绩和人数是核心数据,适合作为全局变量处理
int score[50];    //将score设为全局变量,在各个函数中可以直接使用
int num=50;        //小组人数也设为全局变量
void input_score();
int get_max_score();
int get_min_score();
double get_avg_score();
int count(int);
void output_index(int);
double get_stdev_score();
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);  
  system("PAUSE");
  return 0;
}

// input_score函数提供给同学们参考
//input_score函数的功能是输入小组成员的成绩
void input_score()
{
   int i = 0;
   while((score[i] > 0||score[i] < 100) && i < num )
   {
      cout<<"输入第 "<<i<<" 位同学的成绩:";
      cin>>score[i++];
   }

 }
 
// get_max_score()函数的功能是求出num名同学的最高成绩
int get_max_score()
{
    int i = 0;
 int  b = score[0];
 while(i< num)
 {
  if(b < score[i])
  {
          b = score[i];
  }
   i++;
 }
 return b;


}

// get_min_score()函数的功能是求出num名同学的最低成绩
int get_min_score()
{
 int i = 0;
 int b = score[0];
 while(i < num)
 {
  if(b > score[i])
  {
          b = score[i];
  }
   i++;
 }
 return b;

 

}

// get_avg_score()函数的功能是求出num名同学的平均成绩
double get_avg_score()
{
 double avg;
 double m=0;
 int i;
 for(i=0;i<num;i++)
  m=m+score[i];
 avg=m/num;
 return avg;

 


}

// get_ stdev _score()函数的功能是求出num名同学成绩的标准偏差
double get_stdev_score()
{
 double stdev;
 double p=0;
 for(int i=0;i<num;i++)
  p=p+(score[i]-get_avg_score())*(score[i]-get_avg_score());
 stdev=sqrt(p/(num-1));
 return stdev;

 

 


}

// count(int s)函数的功能是返回值score数组中为s的元素的个数
int count(int s)
{
 int n=0,i=0;
 while(i<num)
 {
  if(score[i]==s)
  {
  n++;
  }
  i++;
 }
 return n;

 

}


// output_index函数的功能是输出score数组中值为s的元素的下标(index)
//注意:值为s的元素可能有多个
void output_index(int s)
{
 int i = 0;
 while(i < num)
 {
  if(score[i] == s)
    cout <<i<<endl;
  i++;
 
 }
}

运行结果:(贴图)

经验积累:
1.在另一函数中用到其它定义乐得函数时要用其全名如本次的平均数
2.
3.

抱歉!评论已关闭.