home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12329 < prev    next >
Encoding:
Text File  |  1992-08-14  |  2.7 KB  |  109 lines

  1. Path: sparky!uunet!munnari.oz.au!mel.dit.csiro.au!yarra!bohra.cpg.oz.au!daavid
  2. From: daavid@bohra.cpg.oz.au (Daavid Turnbull)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: qsort problem....
  5. Message-ID: <1992Aug14.133320.28769@bohra.cpg.oz.au>
  6. Date: 14 Aug 92 13:33:20 GMT
  7. References: <1992Aug10.184635.11618@tin.monsanto.com>
  8. Organization: Computer Power Software
  9. Lines: 98
  10.  
  11. In article <1992Aug10.184635.11618@tin.monsanto.com> bcschu@skws02.monsanto.com (Brett Schultz) writes:
  12. > Hello, for some reason I am having a problem with a qsort system call.  
  13. >
  14. > Here is a test program similar to what I am trying to do:
  15. >
  16. > #include <stdio.h>
  17. > #include <stdlib.h>
  18. > typedef struct my_float {
  19. >     float f;
  20. > }my_float;
  21.  
  22. /*
  23. **    first add the correct prototype
  24. */
  25. int qs_compare(const    void *a,const    void *b)
  26. >
  27. >
  28. >
  29. > main()
  30. > {
  31. >   my_float mf[5];
  32. >   int i;
  33. >   
  34. >   mf[0].f = .5444;
  35. >   mf[1].f = .345;
  36. >   mf[2].f = .976;
  37. >   mf[3].f = .764;
  38. >   mf[4].f = .45345;
  39. >
  40. >   for(i=0; i < 5; ++i)
  41. >     printf("mf[%d] = %f\n",i,mf[i].f);
  42. >
  43. >   printf("\n");
  44. >
  45. >   /*qsort((void *) mf, 5, sizeof(my_float), qs_compare);*/
  46. >
  47. >   for(i=0; i < 5; ++i)
  48. >     printf("mf[%d].f = %f\n",i,mf[i].f);
  49. > }
  50. >
  51. /*
  52. **    Then make the function match the prototype (both qsort()'s and itself)
  53. */
  54. > int qs_compare(const    void *a_,const    void *b_)
  55. > {
  56.     /*
  57.     **    assign the passed args to variables of the correct type
  58.     */
  59.     myfloat *a = (myfloat) a_;
  60.     myfloat *b = (myfloat) b_;
  61.  
  62. >    if (a->f < b->f)
  63. >       return (-1);
  64. >    else if (a->f == b->f)
  65. >       return (0);
  66. >    else
  67. >       return (1);
  68. >  }
  69. >
  70. > I want to sort an array of structures by a float in the structure.
  71. >
  72. > This is not compiling.  Here are the errors:
  73. >
  74. [... unusually informative list of errors for this pretty common problem
  75. deleted...]
  76. >
  77. > Brett
  78. This at first feels like a clumbsy solution but you get passed that.
  79. The thing that pisses me off is that the most readable version of
  80. the solution, (IMHO the one I have presented) creates two extra variables,
  81. which are a complete waste of data.
  82.  
  83. > I want to sort an array of structures by a float in the structure.
  84.  
  85. A clever compiler might optimise this out.
  86.  
  87. The other solution which is probably more efficient and is perfectly
  88. readable in this instance where the sort routine is short defines
  89. your sort function :
  90.  
  91. int qs_compare(const    void *a,  const    void *b)
  92. {
  93.     if ((my_double *a)->f < (my_double *b)->f)
  94.        return (-1);
  95.     else if ((my_double *a)->f == (my_double *b)->f)
  96.        return (0);
  97.     else
  98.        return (1);
  99. }
  100.  
  101. Thats ansi for you,
  102.  
  103. daavid
  104. -- 
  105.     Daavid Turnbull
  106.     daavid@bohra.cpg.oz   +61 3 823 0222    (fax) +61 3 824 8068
  107.     uunet!munnari!bohra.cpg.oz!daavid
  108.     CP Software Export Pty Ltd                   ACN 006 640 133
  109.