c语言:输入n(4<=n<=20)个学生的C语言课程的成绩,请从将分数从高到底进行排序显示,并分别统计100-90,89-60,60以下这三个分数段的学生人数。
时间: 2024-10-13 10:10:18 浏览: 36
在C语言中,你可以使用结构体(struct)来表示学生的信息,包括成绩。然后利用数组来存储多个学生的数据,再通过qsort函数对成绩进行排序。最后,遍历数组统计各个分数段的人数。下面是一个简单的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义结构体表示学生信息
typedef struct {
int score;
char name[50];
} Student;
// 比较函数用于qsort排序
int compare(const void *a, const void *b) {
return ((Student *)a)->score - ((Student *)b)->score;
}
void display_students(Student students[], int n) {
printf("Sorted Students by Score:\n");
for (int i = 0; i < n; i++) {
printf("%d - %s\n", students[i].score, students[i].name);
}
}
void count_grades(Student students[], int n) {
int grades[3] = {0}; // 分别存放三个区间的学生数
for (int i = 0; i < n; i++) {
if (students[i].score >= 90 && students[i].score <= 100)
grades[0]++;
else if (students[i].score >= 60 && students[i].score < 90)
grades[1]++;
else
grades[2]++;
}
printf("\nNumber of students with scores:\n");
printf("90-100: %d\n", grades[0]);
printf("60-89: %d\n", grades[1]);
printf("Below 60: %d\n", grades[2]);
}
int main() {
int n;
printf("Enter number of students (4 to 20): ");
scanf("%d", &n);
if (n < 4 || n > 20) {
printf("Invalid input! Please enter a value between 4 and 20.\n");
return 1;
}
Student* student_array = malloc(n * sizeof(Student));
for (int i = 0; i < n; i++) {
printf("Enter student %d's score: ", i+1);
scanf("%d", &student_array[i].score);
printf("Enter their name: ");
scanf("%s", student_array[i].name);
}
qsort(student_array, n, sizeof(Student), compare);
display_students(student_array, n);
count_grades(student_array, n);
free(student_array);
return 0;
}
```
运行这个程序后,它会提示用户输入学生的数量以及成绩和姓名,然后按照成绩高低排序并计算各分数段的学生人数。
阅读全文