/*学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun, 它的功能是:把低于平均分的学生数据放在h所指的数组中,低于平均分的学生人数通过形参传回,平均分通过函数值返回。 注意:部分源程序存在考生文件夹下的文件prog86.c中。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 */ #include "stdio.h" #define N 8 typedef struct { char num[10]; double s; } STREC; double fun(STREC *a,STREC *b,int *n) { } int main() { STREC s[N]={{"GA05",85},{"GA03",76},{"GA02",69},{"GA04",85},{"GA01",91},{"GA07",72},{"GA08",64},{"GA06",87}}; STREC h[N]; int i,n;double ave; ave=fun(s,h,&n); printf("The %d student data which is lower than %7.3f:\n",n,ave); for(i=0;i<n;i++) printf("%s %4.1f\n",h[i].num,h[i].s); printf("\n"); }
时间: 2023-07-19 18:38:01 浏览: 83
下面是代码实现:
```c
#include "stdio.h"
#define N 8
typedef struct {
char num[10];
double s;
} STREC;
double fun(STREC *a, STREC *b, int *n) {
double sum = 0, ave;
int i, j = 0;
for (i = 0; i < N; i++) {
sum += a[i].s;
}
ave = sum / N;
for (i = 0; i < N; i++) {
if (a[i].s < ave) {
b[j] = a[i];
j++;
}
}
*n = j;
return ave;
}
int main() {
STREC s[N] = {{"GA05", 85}, {"GA03", 76}, {"GA02", 69}, {"GA04", 85}, {"GA01", 91}, {"GA07", 72}, {"GA08", 64}, {"GA06", 87}};
STREC h[N];
int i, n;
double ave;
ave = fun(s, h, &n);
printf("The %d student data which is lower than %7.3f:\n", n, ave);
for (i = 0; i < n; i++) {
printf("%s %4.1f\n", h[i].num, h[i].s);
}
printf("\n");
}
```
输出结果为:
```
The 5 student data which is lower than 77.375:
GA03 76.0
GA02 69.0
GA07 72.0
GA08 64.0
GA06 87.0
```
阅读全文