c - How to properly use qsort()? -


i trying write simple program find median of array. first tried use qsort put values in order can't quite work. believe has function parameter , pointers.

#include <stdio.h> #include <stdlib.h>   int cmpfunc (const void * a, const void * b) {     return ( *(int*)a - *(int*)b ); }  int median(const int* array) {     double median;     int length =0;     while(array[length])         length++;      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( "%f", median(array)); } 

beside getting wrong value length snippet

 while(array[length])  // array not ended 0     length++;          // length unexpected value 

you need return double function median , need 1 more parameter length.

double median(int* array, const int length){...}   //            ^removed const qualifier , added parameter array size   

call median

double result = median(array, sizeof(array)/sizeof(array[0])) 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -