1.函数fun()功能是计算x所指数组中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**********/
__*aver__=s/N;
d=32767;
for(i=0; i<N; i++)
if(x[i]<*av && *av - x[i]<=d){ //d表示与平均数接近的程度
/**********found**********/
d=*av-x[i]; //通过比较后返回接近程度最高的数组下标
j=__i__;
}
/**********found**********/
return __x[j]__;
}
2.函数fun()的功能是计算x所指向数组中N个数的平均值,将数组中大于平均值的数据移至数组前部,小于等于平均值的数据移至数组后部,平均值做为函数值返回
#include <stdlib.h>
#include <stdio.h>
#define N 10
double fun(double *x)
{ int i, j; double s, av, y[N];
s=0;
for(i=0; i<N; i++) s=s+x[i];
/**********found**********/
av=__s/N__;
for(i=j=0; i<N; i++)
if( x[i]>av ){
/**********found**********/
y[__j++__]=x[i]; //将x数组中大于平均数的数放入y数组中,并重新赋值为-1
x[i]=-1;} //这样,重新遍历x数组的时候只要把不是-1的数再放入y数组就好了
for(i=0; i<N; i++) //y数组前部分:大于平均数的数,后部分:x数组里不为-1的数,即小于平均数的数
/**********found**********/
if( x[i]!= __-10__) y[j++]=x[i];
for(i=0; i<N; i++)x[i] = y[i];
return av;
}
3.fun()函数对数组的周边元素进行处理
for(i=0;i<N;i++)
for(j=0;j<N;j++)
if(i==0||i==N-1||j==0||j==N-1) //下标中有0或者N-1的就是周边数组