利用结构体数组编写程序。要求从键盘输入3个 学生的学号、姓名及课程成绩。计算其平均分,并 输出最高分及获得最高分的学生学号,姓名。
时间: 2024-05-10 15:17:43 浏览: 20
以下是一个简单的C语言程序,可以实现你的要求:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT_NUM 3
#define MAX_NAME_LEN 20
typedef struct {
int id;
char name[MAX_NAME_LEN + 1];
float score;
} Student;
int main() {
Student students[MAX_STUDENT_NUM];
float average_score = 0;
float max_score = 0;
int max_score_index = -1;
// 读入学生信息
printf("请输入%d个学生的学号、姓名和成绩:\n", MAX_STUDENT_NUM);
for (int i = 0; i < MAX_STUDENT_NUM; i++) {
printf("学生%d:\n", i + 1);
scanf("%d %s %f", &students[i].id, students[i].name, &students[i].score);
average_score += students[i].score;
if (students[i].score > max_score) {
max_score = students[i].score;
max_score_index = i;
}
}
// 计算平均分
average_score /= MAX_STUDENT_NUM;
// 输出结果
printf("平均分为:%.2f\n", average_score);
printf("最高分为:%.2f,获得最高分的学生是:%d %s\n", max_score, students[max_score_index].id, students[max_score_index].name);
return 0;
}
```
程序中定义了一个结构体类型 `Student`,包含学生的学号、姓名和成绩。定义了一个结构体数组 `students`,用于存储输入的学生信息。程序先通过循环读入学生信息,并计算出平均分和最高分及其对应的学生。最后输出结果。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![asm](https://img-home.csdnimg.cn/images/20210720083646.png)