求平均值把小于平均值的数放在前边大于的放在后边然后输出。
#include <stdlib.h>
#include <stdio.h>
#define N 10
double fun(double *x)
{ int i, j; double av, y[N];
av=0;
for(i=0; i<N; i++)
/**********found**********/
av+=x[i]/N;
for(i=j=0; i<N; i++)
if( x[i]<av ){
y[j]=x[i]; x[i]=-1;//这个是什么意思??
/**********found**********/
j++;//这是什么意思???
}
i=0;
while(i<N)
{ if( x[i]!= -1 ) y[j++]=x[i];
/**********found**********/
i++;
}
for(i=0; i<N; i++)x[i] = y[i];
return av;
}
void main()
{ int i; double x[N];
for(i=0; i<N; i++){ x[i]=rand()%50; printf("%4.0f ",x[i]);}
printf(" ");
printf(" The average is: %f ",fun(x));
printf(" The result : ",fun(x));
for(i=0; i<N; i++) printf("%5.0f ",x[i]);
printf(" ");
}
写这个程序的人用了非平常的方法(一般应该用排序的)
y[j]=x[i]; //将比平均值的的数放到新数组中
x[i]=-1; //在原数组中删除,-1是删除标记。这个程序不适合有-1的数据
j++; //新数组下标加1