Thursday, 19 September 2013

QSorting an array of doubles

QSorting an array of doubles

I'm new to using qsort, and I'm attempting to qsort a predefined array of
doubles, unfortunately the result I receive is all 0's after the array has
been sorted using qsort. I'm completely lost, and any advice would be
greatly appreciated. Here is the code I am using to sort this array of
doubles.
static int compare (const void * a, const void * b){
if (*(const double*)a > *(const double*)b) return 1;
else if (*(const double*)a < *(const double*)b) return -1;
else return 0;
}
double stuff[] = {255, 1, 5, 39.0};
qsort(stuff, 4, sizeof(double), compare);
int i;
for(i = 0; i < 4; i++){
printf("%d %s", stuff[i], " ");
}

No comments:

Post a Comment