高手们 求解为何出现这样的问题????
//一个班级有n个学生,n由用户输入,每个学生有// 学号1-n,有三门成绩(语、数、外)成绩由随机数生成(0到
// 100之间),每个学生的成绩由动态数组保存。
// 要求:
// (1). 取得总分最高的学生的学号。
// (2). 提示用户输入1,2,3来获得语、数、外的平均分。
#include <iostream>
#include <ctime>
using namespace std;
typedef struct student
{
int id;
int chinese_score;
int math_score;
int english_score;
int total;
}stu;
void main()
{
time_t T;
stu *array;
int number;
int chn_total=0,mat_total=0,eng_total=0;
float chn_aver;
float mat_aver;
float eng_aver;
int i(1);
srand((unsigned) time(&T));
cout<<"请输入学生的数量(n)"<<endl;
cin>>number;
array = new stu[number];
for (i=1;i<=number;i++)
{
array[i].id = i;
array[i].chinese_score = rand()%100;
array[i].math_score = rand()%100;
array[i].english_score = rand()%100;
array[i].total = array[i].chinese_score+array[i].math_score+array[i].english_score;
chn_total+=array[i].chinese_score;
mat_total+=array[i].math_score;
eng_total+=array[i].english_score;
}
cout<<"学号"<<"\t"<<"语文成绩"<<"\t"<<"数学成绩"<<"\t"<<"英语成绩"<<"\t"<<"总分"<<endl;
for (i=1;i<=number;i++)
{
cout<<array[i].id<<"\t"<<array[i].chinese_score<<"\t\t"<<array[i].math_score
<<"\t\t"<<array[i].english_score<<"\t\t"<<array[i].total<<endl;
}
int max = array[1].total;
int I=1;
for (i=1;i<=number;i++)
{
if (max<array[i].total)
{
max = array[i].total;
I = i;
}
}
cout<<"最高分的的同学的学号是:"<<I<<endl;
int j(0);
cout<<"显示该班各科成绩的平均分(1:语文平均分,2:数学平均分,3:外语平均分)"<<endl;
cin>>j;
if (1==j)
{
chn_aver =(float)chn_total/number;
cout<<"语文平均分"<<chn_aver<<endl;
}
else if (2==j)
{
mat_aver = (float)mat_total/number;
cout<<"数学平均分"<<mat_aver<<endl;
}
else if (3==j)
{
eng_aver = (float)eng_total/number;
cout<<"外语平均分"<<eng_aver<<endl;
}
else
{
cout<<"error"<<endl;
}
//delete []array;
//array = NULL;
}
[ 本帖最后由 未名湖的云 于 2012-11-22 22:14 编辑 ]