home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_04 / 1104114b < prev    next >
Text File  |  1993-02-05  |  806b  |  43 lines

  1. /* index2.c: Filter out duplicate integers */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define NELEMS 7
  8.  
  9. static int comp(const void *, const void *);
  10.  
  11. static int some_ints[NELEMS] = {40,12,37,12,40,15,15};
  12.  
  13. main()
  14. {
  15.     int last;
  16.     size_t i;
  17.     size_t idx[NELEMS] = {0,1,2,3,4,5,6};
  18.     
  19.     qsort(idx,NELEMS,sizeof idx[0],comp);
  20.  
  21.     /* Output only unique items */
  22.     printf("%d\n",(last = some_ints[idx[0]]));
  23.     for (i = 1; i < NELEMS; ++i)
  24.         if (some_ints[idx[i]] != last)
  25.             printf("%d\n",(last = some_ints[idx[i]]));
  26.     return 0;
  27. }
  28.  
  29. static int comp(const void *p1, const void *p2)
  30. {
  31.     size_t i = * (size_t *) p1;
  32.     size_t j = * (size_t *) p2;
  33.  
  34.     return some_ints[i] - some_ints[j];
  35. }
  36.  
  37. /* Output:
  38. 12
  39. 15
  40. 37
  41. 40
  42. */
  43.