N
No Such Luck
Hi All:
The code below (using the qsort function) produces the following
incorrect result. The last two numbers are not sorted. It this
innaccurate result specific to my compiler's qsort, or is there a bug
in my code? Thanks...
Original Array:
3.125420
8.618710
4.220840
2.181950
8.852060
1.763020
0.164010
Sorted Array:
0.164010
1.763020
2.181950
3.125420
4.220840
8.852060
8.618710
------------------------------------------
#include <stdio.h>
#include <stdlib.h>
typedef int (*qsortfunc) ( const void*, const void* );
int
compare_doubles (const double *a, const double *b)
{
return (int) (*a - *b);
}
int main ()
{
int i;
double array[7];
array[0] = 3.12542;
array[1] = 8.61871;
array[2] = 4.22084;
array[3] = 2.18195;
array[4] = 8.85206;
array[5] = 1.76302;
array[6] = 0.16401;
printf ("\nOriginal Array:\n");
for (i = 0; i < 7; i++)
printf ("%lf\n", array);
qsort(array, 7, sizeof(double),(qsortfunc) compare_doubles);
printf ("\nSorted Array:\n");
for (i = 0; i < 7; i++)
printf ("%lf\n", array);
return 1;
}
The code below (using the qsort function) produces the following
incorrect result. The last two numbers are not sorted. It this
innaccurate result specific to my compiler's qsort, or is there a bug
in my code? Thanks...
Original Array:
3.125420
8.618710
4.220840
2.181950
8.852060
1.763020
0.164010
Sorted Array:
0.164010
1.763020
2.181950
3.125420
4.220840
8.852060
8.618710
------------------------------------------
#include <stdio.h>
#include <stdlib.h>
typedef int (*qsortfunc) ( const void*, const void* );
int
compare_doubles (const double *a, const double *b)
{
return (int) (*a - *b);
}
int main ()
{
int i;
double array[7];
array[0] = 3.12542;
array[1] = 8.61871;
array[2] = 4.22084;
array[3] = 2.18195;
array[4] = 8.85206;
array[5] = 1.76302;
array[6] = 0.16401;
printf ("\nOriginal Array:\n");
for (i = 0; i < 7; i++)
printf ("%lf\n", array);
qsort(array, 7, sizeof(double),(qsortfunc) compare_doubles);
printf ("\nSorted Array:\n");
for (i = 0; i < 7; i++)
printf ("%lf\n", array);
return 1;
}