只是想找到一组数字的中位数,我有以下内容:
double median( int* array, int length)
{
double median;
qsort(array, length, sizeof(int), cmpfunc);
if((length%2) == 0)
{
median = (array[(length/2)] + array[(length/2)-1)])/2;
}
else
{
median =(array[(length -1)/2]);
}
return median;
}
int main ()
{
int array [] = {5, 3, 2, 7, 9};
printf( "%d\n", median (array, sizeof(array)/sizeof(array[0])));
return 0;
}
我的中位数回报是一个疯狂的巨大数字。谁知道为什么?