C语言用数组作为参数,编程计算一组不高于40人的某班级某门课程的平均分、最高分、不及格人数、并进行排序(从高到低),以寝室为单位完成。
时间: 2024-02-18 09:03:40 浏览: 30
好的,以下是用C语言编写的实现:
```c
#include <stdio.h>
#define N 40 // 最多40人
struct Student {
int room; // 寝室号
int score; // 该门课程分数
};
void input_scores(struct Student scores[], int n); // 输入分数
void print_result(struct Student scores[], int n); // 输出结果
float calc_avg(struct Student scores[], int n, int room); // 计算平均分
int calc_fail(struct Student scores[], int n, int room); // 计算不及格人数
void sort_scores(struct Student scores[], int n); // 排序
int main() {
int n; // 学生人数
struct Student scores[N]; // 分数数组
printf("请输入学生人数(不超过40人):");
scanf("%d", &n);
input_scores(scores, n); // 输入分数
print_result(scores, n); // 输出结果
return 0;
}
void input_scores(struct Student scores[], int n) {
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的寝室号和分数(用空格分隔):", i+1);
scanf("%d %d", &scores[i].room, &scores[i].score);
}
}
void print_result(struct Student scores[], int n) {
int rooms[N] = {0}; // 存储已经统计过的寝室号,避免重复统计
sort_scores(scores, n); // 排序
printf("按寝室号排序后的结果如下:\n");
printf("寝室号 平均分 最高分 不及格人数\n");
for (int i = 0; i < n; i++) {
if (rooms[scores[i].room] == 0) { // 如果该寝室号还没有统计过
printf("%d %.2f %d %d\n", scores[i].room, calc_avg(scores, n, scores[i].room),
scores[i].score, calc_fail(scores, n, scores[i].room));
rooms[scores[i].room] = 1; // 标记该寝室号已经统计过
}
}
}
float calc_avg(struct Student scores[], int n, int room) {
float sum = 0;
int count = 0;
for (int i = 0; i < n; i++) {
if (scores[i].room == room) {
sum += scores[i].score;
count++;
}
}
return sum / count;
}
int calc_fail(struct Student scores[], int n, int room) {
int count = 0;
for (int i = 0; i < n; i++) {
if (scores[i].room == room && scores[i].score < 60) {
count++;
}
}
return count;
}
void sort_scores(struct Student scores[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = i+1; j < n; j++) {
if (scores[i].room > scores[j].room) { // 按寝室号升序排序
struct Student temp = scores[i];
scores[i] = scores[j];
scores[j] = temp;
}
}
}
}
```
这个程序中,我们定义了一个 `Student` 结构体,用来存储每个学生的寝室号和分数。`input_scores` 函数用来输入每个学生的寝室号和分数,`print_result` 函数用来输出结果,包括每个寝室号的平均分、最高分和不及格人数,并按照寝室号升序排列。`calc_avg` 函数用来计算某个寝室号的平均分,`calc_fail` 函数用来计算某个寝室号的不及格人数,`sort_scores` 函数用来排序。
注意,因为题目中要求按寝室号为单位统计,所以我们需要先按照寝室号排序,然后统计每个寝室号的平均分、最高分和不及格人数。同时,为了避免重复统计,我们在统计每个寝室号的时候需要记录已经统计过的寝室号。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)