C++使用结构体变量表示每个学生的信息:姓名、学号和三门 课的成绩。从键盘上输入 10 个学生的数据,然后输出每个学生 的姓名和三门课的平均成绩。
时间: 2024-05-08 19:16:59 浏览: 148
```c
#include <stdio.h>
// 定义结构体类型
struct Student {
char name[20];
int number;
float score1;
float score2;
float score3;
};
int main() {
// 定义结构体变量数组
struct Student students[10];
// 从键盘上输入数据
for (int i = 0; i < 10; i++) {
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", students[i].name);
printf("请输入第%d个学生的学号:", i + 1);
scanf("%d", &students[i].number);
printf("请输入第%d个学生的三门课成绩:", i + 1);
scanf("%f%f%f", &students[i].score1, &students[i].score2, &students[i].score3);
}
// 输出每个学生的姓名和三门课的平均成绩
for (int i = 0; i < 10; i++) {
float average = (students[i].score1 + students[i].score2 + students[i].score3) / 3;
printf("%s的平均成绩是%.2f\n", students[i].name, average);
}
return 0;
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.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://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)