用C++写一个学生成绩的程序

班里有20个人,7门课,用键盘输入每个人的7门课程成绩,求出每人平均分并排序,还有求出每门课平均分
不需要什么增删改之类的功能

#include "iostream"

#define STUDENT_COUNT 4
#define SUBJECT_COUNT 2

int main(){
//学生名字
char name[STUDENT_COUNT][20];
//学生分数
int score[STUDENT_COUNT][SUBJECT_COUNT];
//学生均分
float student_aver[STUDENT_COUNT];
//科目均分
double subject_aver[SUBJECT_COUNT];
//学生均分名次序列
int order[STUDENT_COUNT];

//统计成绩
for(int i=0;i<STUDENT_COUNT;i++)
{
std::cout<<"请输入学生名:"<<std::endl;
std::cin>>name[i];
std::cout<<"请输入其"<<SUBJECT_COUNT<<"门成绩:"<<std::endl;
for(int j=0;j<SUBJECT_COUNT;j++)
std::cin>>score[i][j];
}
//求每个学生的均分
for(int i=0;i<STUDENT_COUNT;i++)
{
student_aver[i] = 0;
for(int j=0;j<SUBJECT_COUNT;j++)
student_aver[i]+=score[i][j];
student_aver[i]/=SUBJECT_COUNT;
}

//求每一科目均分
for(int j=0;j<SUBJECT_COUNT;j++)
{
subject_aver[j] = 0 ;
for(int i=0;i<STUDENT_COUNT;i++)
subject_aver[j] += score[i][j];
subject_aver[j]/=STUDENT_COUNT;
}

//对学生均分排序
for(int i=0;i<STUDENT_COUNT;i++)
{
order[i] = 0;
for(int j=0;j<STUDENT_COUNT;j++)
{
if( student_aver[j] > student_aver[order[i]])
order[i] = j;
}
student_aver[order[i]] *= -1;
}

//输出
for(int i=0;i<STUDENT_COUNT;i++)
{
std::cout<<"学生"<<name[order[i]]<<"的平均分为"<<-student_aver[i]<<std::endl;
}
for(int i=0;i<SUBJECT_COUNT;i++)
{
std::cout<<"科目"<<i+1<<"的平均分为"<<subject_aver[i]<<std::endl;
}
std::cout<<"学生成绩排序为:"<<std::endl;
std::cout<<"名次\t姓名\t平均分"<<std::endl;
for(int i=0;i<STUDENT_COUNT;i++)
{
std::cout<<i+1<<'\t'<<name[order[i]]<<'\t'<<-student_aver[order[i]]<<std::endl;
}
system("pause");
return 0;
}

/*
以4个学生2门课为例,20 个学生 7门课只需将
宏定义 STUDENT_COUNT 和 SUBJECT_COUNT后面数字改为20 和 7

请输入学生名:
贝贝
请输入其2门成绩:
98 94
请输入学生名:
晶晶
请输入其2门成绩:
92 89
请输入学生名:
欢欢
请输入其2门成绩:
100 87
请输入学生名:
莹莹
请输入其2门成绩:
89 100
学生贝贝的平均分为96
学生莹莹的平均分为90.5
学生欢欢的平均分为93.5
学生晶晶的平均分为94.5
科目1的平均分为94.75
科目2的平均分为92.5
学生成绩排序为:
名次 姓名 平均分
1 贝贝 96
2 莹莹 94.5
3 欢欢 93.5
4 晶晶 90.5
请按任意键继续. . .
*/
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-12-01
#include <iostream>
#include <string>

using namespace std;

struct Student
{
string name;
int score[7];
};

int main()
{
const int MAX = 2;
Student stu[MAX];

for (int i = 0; i != MAX; ++i)
{
cout << "第"<< i+1 << "位学生姓名: " <<ends;
cin >> stu[i].name;
cout << "第一门课成绩:" << ends;
cin >> stu[i].score[0];
cout << "第二门课成绩:" << ends;
cin >> stu[i].score[1];
cout << "第三门课成绩:" << ends;
cin >> stu[i].score[2];
cout << "第四门课成绩:" << ends;
cin >> stu[i].score[3];
cout << "第五门课成绩:" << ends;
cin >> stu[i].score[4];
cout << "第六门课成绩:" << ends;
cin >> stu[i].score[5];
cout << "第七门课成绩:" << ends;
cin >> stu[i].score[6];
cout << endl;
}
cout << endl;

//每人平均分
int dpj = 0;
cout << "每人平均分: " << endl;
for (int i = 0; i != MAX; ++i)
{
cout << stu[i].name << endl;
dpj = (stu[i].score[0]+stu[i].score[1]+stu[i].score[2]+
stu[i].score[3]+stu[i].score[4]+stu[i].score[5]+
stu[i].score[6]) / 7;
cout << dpj << endl << endl;
}

//课程平均分
for (int j = 0; j != 7; ++j)
{
dpj = 0;
for (int i = 0; i != MAX; ++i)
{
dpj += stu[i].score[j];
}
cout << "第" << j+1 << "门课平均分: " << dpj << endl;
}

return 0;
}

//不知你课程名称!所以以代号代替了本回答被提问者采纳
第2个回答  2008-12-03
#include<iostream>
using namespace std;
void main()
{
int a[20][7],j=0;
int total=0,average,gradeCounter=0,grade,i;
for(i=0;i<20;i++)
{
while(gradeCounter<7)
{
cout<<"Enter grade: ";
cin>>grade;
a[i][gradeCounter]=grade;
total+=grade;
gradeCounter++;
}
average=total/7;
a[i][j]=average;
j++;
}
cout<<"\n每人平均分排序为:";
for(i=0;i<19;i++)
{
for(j=0;j<6;j++)
{
for(int k=1;k<7;k++)
{
if(a[i][j]>a[i][k])
{
int temp=a[i][j];
a[i][j]=a[i][k];
a[i][k]=temp;
}
}
}
for(int u=0;u<7;u++)
{
cout<<a[i][u]<<" ";
}
cout<<endl;
}
cout<<"\n每门课的排序为:";
for(i=0;i<7;i++)
{
for(int x=0;x<19;x++)
{
for(int y=1;y<20;y++)
{
if(a[i][x]>a[i][y])
{
int e=a[i][x];
a[i][x]=a[i][y];
a[i][y]=e;
}
}
}
cout<<a[i][x]<<" ";
}

}