第1题的基础上,定义并初始化struct Student数组stus[10],显示stus数组数据
时间: 2024-03-25 22:36:07 浏览: 24
好的,以下是代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
struct Student {
char id[10]; //学号
char Name[20]; //姓名
double Score[3]; //3门课程成绩
};
int main() {
Student stus[10] = {
{"1001", "张三", {80, 75, 88}},
{"1002", "李四", {85, 90, 92}},
{"1003", "王五", {92, 88, 90}},
{"1004", "赵六", {78, 80, 84}},
{"1005", "钱七", {90, 92, 95}},
{"1006", "孙八", {85, 88, 90}},
{"1007", "周九", {92, 80, 86}},
{"1008", "吴十", {78, 85, 90}},
{"1009", "郑一", {90, 92, 88}},
{"1010", "王二", {85, 80, 92}}
};
//显示数据
for (int i = 0; i < 10; i++) {
cout << "学号:" << stus[i].id << " ";
cout << "姓名:" << stus[i].Name << " ";
cout << "3门课程成绩:";
for (int j = 0; j < 3; j++) {
cout << stus[i].Score[j] << " ";
}
cout << endl;
}
return 0;
}
```
运行结果:
```
学号:1001 姓名:张三 3门课程成绩:80 75 88
学号:1002 姓名:李四 3门课程成绩:85 90 92
学号:1003 姓名:王五 3门课程成绩:92 88 90
学号:1004 姓名:赵六 3门课程成绩:78 80 84
学号:1005 姓名:钱七 3门课程成绩:90 92 95
学号:1006 姓名:孙八 3门课程成绩:85 88 90
学号:1007 姓名:周九 3门课程成绩:92 80 86
学号:1008 姓名:吴十 3门课程成绩:78 85 90
学号:1009 姓名:郑一 3门课程成绩:90 92 88
学号:1010 姓名:王二 3门课程成绩:85 80 92
```