Re: gcc/sort

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



your function indeed needs to return an int, but the computation doesn't look like an int.

you will need to compare, and return one of -1,0,1, not subtract the values.


On 3/5/20 8:22 AM, Patrick Dupre wrote:
Hello,

In this example, if I put a 0, the sorting is wrong which must be standard c

After sorting the list is:
0 -5.5e-06 3 25 56 88 100
-5.5e-06 2 3 25 56 88 100

#include <stdio.h>
#include <stdlib.h>

double values[] = { 88, 56, 100, 0.0, 25, 3, -0.55e-5 };
double values2 [] = { 88, 56, 100, 2, 25, 3, -0.55e-5 };

int dcmpfunc (const void * a, const void * b) {
return ( *(double*)a - *(double*)b );
}

int main () {
int n;
double *val2 = (double*) malloc (7 * sizeof(double)) ;
double *val = (double*) malloc (7 * sizeof(double)) ;
for (n = 0 ; n < 7 ; n++) val [n] = values [n] ;
for (n = 0 ; n < 7 ; n++) val2 [n] = values2 [n] ;

qsort (val, 7, sizeof (double), dcmpfunc);
qsort (val2, 7, sizeof (double), dcmpfunc);

printf ("\nAfter sorting the list is: \n") ;
for (n = 0 ; n < 7; n++ ) printf("%g ", val [n]); printf ("\n") ;
for (n = 0 ; n < 7; n++ ) printf("%g ", val2 [n]); printf ("\n") ;

return(0);
}

===========================================================================
  Patrick DUPRÉ                                 | | email: pdupre@xxxxxxx
  Laboratoire interdisciplinaire Carnot de Bourgogne
  9 Avenue Alain Savary, BP 47870, 21078 DIJON Cedex FRANCE
  Tel: +33 (0)380395988
===========================================================================
_______________________________________________
users mailing list -- users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to users-leave@xxxxxxxxxxxxxxxxxxxxxxx
Fedora Code of Conduct: https://docs.fedoraproject.org/en-US/project/code-of-conduct/
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedoraproject.org/archives/list/users@xxxxxxxxxxxxxxxxxxxxxxx
_______________________________________________
users mailing list -- users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to users-leave@xxxxxxxxxxxxxxxxxxxxxxx
Fedora Code of Conduct: https://docs.fedoraproject.org/en-US/project/code-of-conduct/
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedoraproject.org/archives/list/users@xxxxxxxxxxxxxxxxxxxxxxx



[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux