函数的功能是随机生成一个数组,并计算出数组中的平均值,,再返回数组中的与平均值最接近的数;
**#include <stdlib.h> #include <stdio.h> #define N 10 double fun(double x[],double *av) { int i,j; double d,s; s=0; for(i=0; i<N; i++) s = s +x[i]; /**********found**********/ __1__=s/N; d=32767; for(i=0; i<N; i++) if(x[i]<*av && *av - x[i]<=d){ /**********found**********/ d=*av-x[i]; j=__2__; } /**********found**********/ return __3__; } main() { int i; double x[N],av,m; for(i=0; i<N; i++){ x[i]=rand()%50; printf("%4.0f ",x[i]);} printf("\n"); m=fun(x,&av); printf("\nThe average is: %f\n",av); printf("m=%5.1f ",m); printf("\n"); } ** **#include <stdlib.h> #include <stdio.h> #define N 10 double fun(double x[],double *av) { int i,j; double d,s; s=0; for(i=0; i<N; i++) s = s +x[i]; /**********found**********/ *av=s/N; d=32767; for(i=0; i<N; i++) if(x[i]<*av && *av - x[i]<=d){ /**********found**********/ d=*av-x[i]; j=x[i]; } /**********found**********/ return j; } main() { int i; double x[N],av,m; for(i=0; i<N; i++){ x[i]=rand()%50; printf("%4.0f ",x[i]);} printf("\n"); m=fun(x,&av); printf("\nThe average is: %f\n",av); printf("m=%5.1f ",m); printf("\n"); } ** **E:\Clion_file\pro.1\cmake-build-debug\pro_1.exe 41 17 34 0 19 24 28 8 12 14 The average is: 19.700000 m= 19.0 Process finished with exit code 0**