为我呈现完整代码
时间: 2024-02-18 08:03:35 浏览: 19
好的,这是完整的代码:
```c++
#include <iostream>
#include <cmath>
using namespace std;
struct student {
int scores[5];
float avg_score;
};
void input_scores(student* students, int num_students) {
for (int i = 0; i < num_students; i++) {
cout << "Enter scores for student " << i+1 << ": ";
for (int j = 0; j < 5; j++) {
cin >> students[i].scores[j];
}
}
}
void calculate_avg_student_score(student* students, int num_students) {
for (int i = 0; i < num_students; i++) {
float sum = 0;
for (int j = 0; j < 5; j++) {
sum += students[i].scores[j];
}
students[i].avg_score = sum / 5;
}
}
void calculate_avg_course_score(student* students, int num_students) {
float sum_scores[5] = {0};
for (int i = 0; i < num_students; i++) {
for (int j = 0; j < 5; j++) {
sum_scores[j] += students[i].scores[j];
}
}
for (int i = 0; i < 5; i++) {
cout << "Average score for course " << i+1 << " is: " << sum_scores[i] / num_students << endl;
}
}
void find_highest_score(student* students, int num_students) {
int max_score = 0;
int max_student = 0;
int max_course = 0;
for (int i = 0; i < num_students; i++) {
for (int j = 0; j < 5; j++) {
if (students[i].scores[j] > max_score) {
max_score = students[i].scores[j];
max_student = i+1;
max_course = j+1;
}
}
}
cout << "The highest score is " << max_score << " and it belongs to student " << max_student << " and course " << max_course << endl;
}
void calculate_variance(student* students, int num_students) {
float sum = 0;
float sum_squared = 0;
for (int i = 0; i < num_students; i++) {
sum += students[i].avg_score;
sum_squared += pow(students[i].avg_score, 2);
}
float variance = sum_squared / num_students - pow(sum / num_students, 2);
cout << "The variance of the average scores is: " << variance << endl;
}
int main() {
int num_students = 10;
student students[num_students];
input_scores(students, num_students);
calculate_avg_student_score(students, num_students);
calculate_avg_course_score(students, num_students);
find_highest_score(students, num_students);
calculate_variance(students, num_students);
return 0;
}
```
希望对您有所帮助!