/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:刘畅
* 完成日期:2014 年 11 月 26 日
* 版 本 号:v1.0
*
* 问题描写:某班不超过100名同学,用2维数组score[ ][4]保存同学们的高数、英语、C++成绩及总成绩(在此假定学生的学号为整形的连续值,用数组的行下标作学号)。在此基础上,完成相应的要求;
* 输入描写:(1)输入学生人数、学生3科的成绩,(2)还要输入学生姓名;
* 程序输出:输出各门课及总分的最高成绩、最低成绩、平均成绩和最高分数取得者。
(1)输入学生的实际人数num,在输入各科的成绩时,输入3科成绩后可以自动求总分,并将数据全保存到数组中;
输出各门课的总分的最高成绩、最低成绩、平均成绩;
输入代码:
#include <iostream>
#include <string>
using namespace std;
void input(double s[][4], int n);
void output(double s[][4], int n);
double max(double s[][4], int n, int i);
double min(double s[][4], int n, int i);
double avg(double s[][4], int n, int i);
int main()
{
int i, num;
string temp;
double score[100][4];
string course[4] = { "高等数学", "英语", "C++", "总分" };
cout << "输入学生人数:";
cin >> num;
input(score, num);
output(score, num);
for (i = 0; i < 4; ++i)
{
cout << course[i] << "的最高成绩是" << max(score, num,
i) << ", ";
cout << "最低成绩是" << min(score, num, i) << ", ";
cout << "平均成绩是" << avg(score, num, i) << ", ";
cout << endl;
}
return 0;
}
void input(double s[][4], int n)
{
int i, j;
for (i = 0, s[i][3] = 0; i < n; ++i)
{
cout<<"第"<<i+1<<"位 ";
for (j = 0; j < 3; ++j)
{
cin >> s[i][j];
s[i][3] += s[i][j];
}
}
}
void output(double s[][4], int n)
{
int i;
for (i = 0; i < n; ++i)
cout << "第" << i +
1 << "位" << " 高等数学: " << s[i][0] << " 英语: " <<
s[i][1] << " C++: " << s[i][2] << " 总分: " << s[i][3] << endl;
}
double max(double s[ ][4],int n,int i)
{
int m,max=0;
for (m=0;m<n;++m)
{
if (s[m][i]>max)
max=s[m][i];
}
return max;
}
double min(double s[ ][4],int n,int i)
{
int m,min=10000;
for (m=0;m<n;++m)
{
if (s[m][i]<min)
min=s[m][i];
}
return min;
}
double avg(double s[ ][4],int n,int i)
{
int m;
double sum=0;
for (m=0;m<n;++m)
{
sum+=s[m][i];
}
return sum/n;
}
运行结果:
(2) 增加用数组string name[ ]表示同学们的姓名,使姓名与成绩的下标保持1致。
请在写上面的程序的基础上,输出取得最高成绩的同学的名单( 有可能并列第1名哦);
输入代码:
#include <iostream>
#include <string>
using namespace std;
void input(double s[][4], int n);
void output(double s[][4], int n);
double max(double s[][4], int n, int i);
double min(double s[][4], int n, int i);
double avg(double s[][4], int n, int i);
int main()
{
int i, num,m;
string temp;
double score[100][4];
string course[4] = { "高等数学", "英语", "C++", "总分" };
cout << "输入学生人数:";
cin >> num;
string name[num];
cout<<"顺次输入学生姓名:";
for (i=0; i<num; ++i)
cin>>name[i];
input(score, num);
output(score, num);
for (i = 0; i < 4; ++i)
{
cout << course[i] << "的最高成绩是" << max(score, num, i) << ", ";
cout << "最低成绩是" << min(score, num, i) << ", ";
cout << "平均成绩是" << avg(score, num, i) << ", ";
cout<< "最高成绩取得者";
for (m=0; m<num; ++m)
if (score[m][i]==max(score,num,i))
{
cout<<name[m]<<" ";
}
cout << endl;
}
return 0;
}
void input(double s[][4], int n)
{
int i, j;
for (i = 0, s[i][3] = 0; i < n; ++i)
{
cout<<"第"<<i+1<<"位 ";
for (j = 0; j < 3; ++j)
{
cin >> s[i][j];
s[i][3] += s[i][j];
}
}
}
void output(double s[][4], int n)
{
int i;
for (i = 0; i < n; ++i)
cout << "第" << i +
1 << "位" << " 高等数学: " << s[i][0] << " 英语: " <<
s[i][1] << " C++: " << s[i][2] << " 总分: " << s[i][3] << endl;
}
double max(double s[ ][4],int n,int i)
{
int m,max=0;
for (m=0; m<n; ++m)
{
if (s[m][i]>max)
max=s[m][i];
}
return max;
}
double min(double s[ ][4],int n,int i)
{
int m,min=10000;
for (m=0; m<n; ++m)
{
if (s[m][i]<min)
min=s[m][i];
}
return min;
}
double avg(double s[ ][4],int n,int i)
{
int m;
double sum=0;
for (m=0; m<n; ++m)
{
sum+=s[m][i];
}
return sum/n;
}
运行结果:
学习心得:
在写代码的时候犯了1个小毛病,对min的初始化设为了100,没有斟酌总分的最小值是大于100的,所以运行的时候没有得到想要的结果,好在及时发现并改正。。。