2.按照以下要求,定义和初始化3个学生信息和三门课程的成绩,并输出学生的全部信息,每个学生的总分和平均分。
时间: 2024-03-05 19:51:08 浏览: 25
好的,根据您的要求,我可以使用 C++ 语言来实现这个功能。以下是代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int id;
int scores[3];
float totalScore;
float avgScore;
};
int main() {
// 初始化三个学生信息
Student students[3];
students[0].name = "张三";
students[0].id = 101;
students[0].scores[0] = 80;
students[0].scores[1] = 85;
students[0].scores[2] = 90;
students[1].name = "李四";
students[1].id = 102;
students[1].scores[0] = 75;
students[1].scores[1] = 88;
students[1].scores[2] = 92;
students[2].name = "王五";
students[2].id = 103;
students[2].scores[0] = 90;
students[2].scores[1] = 85;
students[2].scores[2] = 95;
// 计算每个学生的总分和平均分
for (int i = 0; i < 3; i++) {
students[i].totalScore = students[i].scores[0] + students[i].scores[1] + students[i].scores[2];
students[i].avgScore = students[i].totalScore / 3.0;
}
// 输出每个学生的信息、总分和平均分
for (int i = 0; i < 3; i++) {
cout << "姓名:" << students[i].name << endl;
cout << "学号:" << students[i].id << endl;
cout << "成绩:" << students[i].scores[0] << " " << students[i].scores[1] << " " << students[i].scores[2] << endl;
cout << "总分:" << students[i].totalScore << endl;
cout << "平均分:" << students[i].avgScore << endl << endl;
}
return 0;
}
```
输出结果如下:
```
姓名:张三
学号:101
成绩:80 85 90
总分:255
平均分:85
姓名:李四
学号:102
成绩:75 88 92
总分:255
平均分:85
姓名:王五
学号:103
成绩:90 85 95
总分:270
平均分:90
```
希望我的回答能够帮到您!
相关推荐
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)