输入10个学生5门课的成绩,分别用函数实现下列功能: ①计算每个学生的平均分; ②计算每门课的平均分; ③找出所有50个分数中最高的分数所对应的学生和课程; ④计算平均分方差: 其中,x:为某一学生的平均分。使用C++使用结构体,函数与指针
时间: 2024-01-01 22:05:05 浏览: 90
好的,下面是代码实现:
```c++
#include<iostream>
#include<cmath>
using namespace std;
const int N = 10, M = 5;
struct Student {
int score[M];
double avg;
};
double getAvg(Student* st) { // 计算每个学生的平均分
double sum = 0;
for (int i = 0; i < M; i++) {
sum += st->score[i];
}
st->avg = sum / M;
return st->avg;
}
double getAvgCourse(Student* st[], int idx) { // 计算每门课的平均分
double sum = 0;
for (int i = 0; i < N; i++) {
sum += st[i]->score[idx];
}
return sum / N;
}
void findMax(Student* st[], int& maxScore, int& maxStuIdx, int& maxCourseIdx) { // 找出最高分数所对应的学生和课程
maxScore = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (st[i]->score[j] > maxScore) {
maxScore = st[i]->score[j];
maxStuIdx = i;
maxCourseIdx = j;
}
}
}
}
double getVariance(Student* st[]) { // 计算平均分方差
double avg = 0, sum = 0;
for (int i = 0; i < N; i++) {
avg += st[i]->avg;
}
avg /= N;
for (int i = 0; i < N; i++) {
sum += pow(st[i]->avg - avg, 2);
}
return sum / N;
}
int main() {
Student students[N];
for (int i = 0; i < N; i++) {
cout << "请输入第" << i + 1 << "个学生的5门课成绩:" << endl;
for (int j = 0; j < M; j++) {
cin >> students[i].score[j];
}
getAvg(&students[i]);
}
cout << "每个学生的平均分:" << endl;
for (int i = 0; i < N; i++) {
cout << "第" << i + 1 << "个学生平均分为:" << students[i].avg << endl;
}
cout << "每门课的平均分:" << endl;
for (int i = 0; i < M; i++) {
cout << "第" << i + 1 << "门课平均分为:" << getAvgCourse(students, i) << endl;
}
int maxScore, maxStuIdx, maxCourseIdx;
findMax(students, maxScore, maxStuIdx, maxCourseIdx);
cout << "最高分数为:" << maxScore << ",对应的学生为第" << maxStuIdx + 1 << "个,课程为第" << maxCourseIdx + 1 << "门" << endl;
cout << "平均分方差为:" << getVariance(students) << endl;
return 0;
}
```
代码解释:
1. 定义了一个结构体 `Student`,其中包含了每个学生的5门课成绩和平均分。
2. `getAvg` 函数用于计算每个学生的平均分。
3. `getAvgCourse` 函数用于计算每门课的平均分,需要传入一个指向 `Student` 数组的指针以及要计算的课程的下标。
4. `findMax` 函数用于找出最高分数所对应的学生和课程,需要传入一个指向 `Student` 数组的指针以及三个引用类型的参数,分别表示最高分数、最高分数所对应的学生的下标和最高分数所对应的课程的下标。
5. `getVariance` 函数用于计算平均分方差,需要传入一个指向 `Student` 数组的指针。
6. 在 `main` 函数中,首先读入每个学生的成绩,并计算每个学生的平均分;然后分别输出每个学生的平均分、每门课的平均分、最高分数所对应的学生和课程,以及平均分方差。
注意:本代码只是一个简单的示例,没有进行输入的合法性检查。在实际使用中应该加上相应的检查。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)