随机产生30个学生的C语言成绩(成绩范围在[0,100]之间),统计并输出不及格(<60分)的人数,并按照从大到小的顺序将数组所有元素进行排序,分别显示排序前和排序后的结果,每行输出10个。
时间: 2023-05-15 11:05:24 浏览: 176
好的,我可以帮您解决这个问题。以下是代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_STUDENTS 30
#define MIN_SCORE 0
#define MAX_SCORE 100
#define PASS_SCORE 60
void print_array(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
if ((i + 1) % 10 == 0) {
printf("\n");
}
}
printf("\n");
}
int main() {
int scores[NUM_STUDENTS];
int num_fail = 0;
srand(time(NULL));
for (int i = 0; i < NUM_STUDENTS; i++) {
scores[i] = rand() % (MAX_SCORE - MIN_SCORE + 1) + MIN_SCORE;
if (scores[i] < PASS_SCORE) {
num_fail++;
}
}
printf("Number of students who failed: %d\n", num_fail);
printf("Scores before sorting:\n");
print_array(scores, NUM_STUDENTS);
// Bubble sort
for (int i = 0; i < NUM_STUDENTS - 1; i++) {
for (int j = 0; j < NUM_STUDENTS - i - 1; j++) {
if (scores[j] < scores[j + 1]) {
int temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
printf("Scores after sorting:\n");
print_array(scores, NUM_STUDENTS);
return 0;
}
```
希望这个代码可以帮到您!
阅读全文