有10个学生,每个学生的数据包括学号、姓名、3门课程的成绩。从键盘输入10个学生的数据 要求输出 3 门课程的总平均成绩,以及最高分的学生的学号、姓名、3门课程成绩、平均分数。(定义结构体)
时间: 2023-12-15 21:28:08 浏览: 73
以下是C++代码:
#include <iostream>
#include <string>
using namespace std;
struct Student {
int id;
string name;
double score[3];
double avgScore;
};
int main() {
const int numStudents = 10;
Student students[numStudents];
// 从键盘输入学生数据
for (int i = 0; i < numStudents; i++) {
cout << "输入第" << i+1 << "个学生的学号、姓名、3门课程的成绩:" << endl;
cin >> students[i].id >> students[i].name;
double sumScore = 0;
for (int j = 0; j < 3; j++) {
cin >> students[i].score[j];
sumScore += students[i].score[j];
}
students[i].avgScore = sumScore / 3.0;
}
// 计算平均成绩和最高分的学生
double sumScores[3] = {0, 0, 0};
double maxScore = 0;
int maxScoreIndex = 0;
for (int i = 0; i < numStudents; i++) {
sumScores[0] += students[i].score[0];
sumScores[1] += students[i].score[1];
sumScores[2] += students[i].score[2];
if (students[i].avgScore > maxScore) {
maxScore = students[i].avgScore;
maxScoreIndex = i;
}
}
double avgScores[3] = {sumScores[0] / numStudents, sumScores[1] / numStudents, sumScores[2] / numStudents};
// 输出结果
cout << "3门课程的总平均成绩为:" << avgScores[0] << " " << avgScores[1] << " " << avgScores[2] << endl;
cout << "最高分的学生信息:" << endl;
cout << "学号:" << students[maxScoreIndex].id << endl;
cout << "姓名:" << students[maxScoreIndex].name << endl;
cout << "成绩:" << students[maxScoreIndex].score[0] << " " << students[maxScoreIndex].score[1] << " " << students[maxScoreIndex].score[2] << endl;
cout << "平均分数:" << students[maxScoreIndex].avgScore << endl;
return 0;
}
阅读全文