C语言考试题大神求代码啊!!!!输入科目数、学生个数、各科成绩,计算平均分。

2024-12-19 14:08:39
推荐回答(1个)
回答1:

#include
using namespace std;
int main()
{
int num, stu;
cout << "how many subjects and how many students:" << endl;
cin >> num >> stu;
while (cin.fail())/*输入数据不匹配处理,这里我就只处理一个,后面的就不写了,反正对于你来说这个也算多余的。*/
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "how many subjects and how many students:" << endl;
cin >> num >> stu;
}
//指针分配内存
double *pn = new double[stu*num];
double *pb = pn;
for (int i = 0; i < stu; i++)
{
cout << "第:" << i + 1 << "个学生成绩:";
for (int j = 0; j < num; j++)
{
cin >> *pb++;
}
}
double value = 0;
pb = pn;
for (int i = 0; i < stu; i++)
{
for (int j = 0; j < num; j++)
value += *pb++;
cout << "第:" << i + 1 << "个学生平均成绩:" << value / num << endl;
value = 0;
}
delete[] pn;
pn = NULL;
return 0;
}