用结构体记录学号和成绩,输出最好成绩的学生信息,并且输出最高分和平均分
时间: 2023-05-20 21:02:19 浏览: 86
可以使用以下代码实现:
```
#include <stdio.h>
#define MAX_STUDENTS 100
struct Student {
int id;
float score;
};
int main() {
struct Student students[MAX_STUDENTS];
int num_students;
float max_score = 0;
float total_score = 0;
printf("Enter the number of students: ");
scanf("%d", &num_students);
for (int i = 0; i < num_students; i++) {
printf("Enter student %d's ID: ", i + 1);
scanf("%d", &students[i].id);
printf("Enter student %d's score: ", i + 1);
scanf("%f", &students[i].score);
if (students[i].score > max_score) {
max_score = students[i].score;
}
total_score += students[i].score;
}
printf("The student with the highest score is:\n");
for (int i = 0; i < num_students; i++) {
if (students[i].score == max_score) {
printf("ID: %d, Score: %.2f\n", students[i].id, students[i].score);
}
}
printf("The highest score is: %.2f\n", max_score);
printf("The average score is: %.2f\n", total_score / num_students);
return 0;
}
```
这个程序使用结构体 `Student` 来记录每个学生的学号和成绩。首先,程序会要求用户输入学生的数量,然后使用 `for` 循环来逐个输入每个学生的信息,并计算出最高分和总分。接着,程序会使用另一个 `for` 循环来输出最高分的学生信息,最高分和平均分。
注意,这个程序只能处理最多 100 个学生的信息。如果需要处理更多的学生,可以将 `MAX_STUDENTS` 宏定义的值改为更大的数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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_column_c1.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)