*学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun, 它的功能是:把指定分数范围内的学生数据放在b所指的数组中,分数范围内的学生人数由函数值返回。 例如,输入的分数是60和69,则应当把分数在60到69的学生数据进行输出,包含60分和69分的学生数据。 主函数中把60放在low中,把69放在heigh中。注意:部分源程序给出如下。 请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句
时间: 2024-04-01 14:38:07 浏览: 166
根据题意,我们需要编写一个函数 `fun`,实现在指定分数范围内查找学生数据,并把符合条件的学生数据放到数组 b 中,最后返回符合条件的学生人数。
以下是一个示例代码:
```c
#include <stdio.h>
#define N 100
typedef struct Student {
int id;
int score;
} Student;
int fun(Student s[], int n, int low, int high, Student b[]) {
int i, count = 0;
for (i = 0; i < n; i++) {
if (s[i].score >= low && s[i].score <= high) {
b[count] = s[i];
count++;
}
}
return count;
}
int main() {
Student s[N];
Student b[N];
int n, low, high, i, count;
printf("请输入学生人数:");
scanf("%d", &n);
for (i = 0; i < n; i++) {
s[i].id = i + 1;
printf("请输入第%d个学生的成绩:", i+1);
scanf("%d", &s[i].score);
}
printf("请输入分数范围:");
scanf("%d %d", &low, &high);
count = fun(s, n, low, high, b);
printf("符合要求的学生人数为:%d\n", count);
printf("符合要求的学生数据为:\n");
for (i = 0; i < count; i++) {
printf("学号:%d,成绩:%d\n", b[i].id, b[i].score);
}
return 0;
}
```
在这个代码中,我们定义了一个函数 `fun`,它的参数包括一个学生数组 s、数组长度 n、分数范围 low 和 high,以及一个用于存储符合条件学生数据的数组 b。在函数中,我们遍历学生数组 s,对于每个学生,如果其成绩在 low 和 high 范围内,则把其数据存储到 b 数组中,并增加计数器 count。最后返回 count 即为符合条件的学生人数。
在主函数中,我们首先输入学生人数和每个学生的成绩,然后输入分数范围,调用函数 `fun` 进行处理,并输出结果。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.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)
![](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://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)