1. 定义一个学生的结构体,该结构体含有5个数据记录,分别为:name, no., score[3];在此基础上,定义个一个含有2个元素的结构体数组并赋值;编写一个printInfo的函数,通过函数调用的方式将这两个学生的信息输出。
时间: 2024-02-22 09:55:36 浏览: 24
好的,我可以帮你解决这个问题。以下是 C++ 代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
// 定义一个学生的结构体
struct Student {
string name; // 姓名
int no; // 学号
int score[3]; // 分数
};
// 定义一个含有2个元素的结构体数组并赋值
Student students[2] = {
{ "Tom", 1, { 90, 80, 85 } },
{ "Jerry", 2, { 95, 85, 90 } }
};
// 输出学生信息的函数
void printInfo(Student s) {
cout << "姓名:" << s.name << endl;
cout << "学号:" << s.no << endl;
cout << "分数:";
for (int i = 0; i < 3; i++) {
cout << s.score[i] << " ";
}
cout << endl;
}
int main() {
// 输出两个学生的信息
for (int i = 0; i < 2; i++) {
cout << "第" << i+1 << "个学生信息:" << endl;
printInfo(students[i]);
}
return 0;
}
```
运行上述代码,将会输出以下结果:
```
第1个学生信息:
姓名:Tom
学号:1
分数:90 80 85
第2个学生信息:
姓名:Jerry
学号:2
分数:95 85 90
```
希望这个问题的解答能够帮助到你。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)