4.使用结构体数组,将学生的数据按成绩从高到低排序显示到屏幕上 将学生中不及格学生的姓名显示出来 写一个函数,获取成绩低于平均分的学生记录并返回低于平均分的学生人数
时间: 2023-05-12 15:03:21 浏览: 84
homewoke13.zip_homewoke
对于第一个问题,可以使用冒泡排序或快速排序等算法来实现。以下是一个使用冒泡排序的示例代码:
```
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
char name[20];
int score;
};
void bubble_sort(struct student students[], int n) {
int i, j;
struct student temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (students[j].score < students[j + 1].score) {
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
void print_failed_students(struct student students[], int n) {
int i;
printf("不及格学生的姓名:\n");
for (i = 0; i < n; i++) {
if (students[i].score < 60) {
printf("%s\n", students[i].name);
}
}
}
int get_below_average_students(struct student students[], int n, float average) {
int i, count = 0;
for (i = 0; i < n; i++) {
if (students[i].score < average) {
count++;
}
}
return count;
}
int main() {
struct student students[MAX_STUDENTS];
int n, i;
float sum = 0, average;
printf("请输入学生人数:");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的姓名和成绩:", i + 1);
scanf("%s%d", students[i].name, &students[i].score);
sum += students[i].score;
}
average = sum / n;
bubble_sort(students, n);
printf("按成绩从高到低排序的学生数据:\n");
for (i = 0; i < n; i++) {
printf("%s\t%d\n", students[i].name, students[i].score);
}
print_failed_students(students, n);
printf("成绩低于平均分的学生人数:%d\n", get_below_average_students(students, n, average));
return 0;
}
```
对于第二个问题,可以遍历学生数组,找到成绩低于60分的学生,然后输出他们的姓名即可。
对于第三个问题,可以遍历学生数组,统计成绩低于平均分的学生人数并返回即可。
阅读全文