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 >
Wrap
Text File
|
1993-02-05
|
806b
|
43 lines
/* index2.c: Filter out duplicate integers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NELEMS 7
static int comp(const void *, const void *);
static int some_ints[NELEMS] = {40,12,37,12,40,15,15};
main()
{
int last;
size_t i;
size_t idx[NELEMS] = {0,1,2,3,4,5,6};
qsort(idx,NELEMS,sizeof idx[0],comp);
/* Output only unique items */
printf("%d\n",(last = some_ints[idx[0]]));
for (i = 1; i < NELEMS; ++i)
if (some_ints[idx[i]] != last)
printf("%d\n",(last = some_ints[idx[i]]));
return 0;
}
static int comp(const void *p1, const void *p2)
{
size_t i = * (size_t *) p1;
size_t j = * (size_t *) p2;
return some_ints[i] - some_ints[j];
}
/* Output:
12
15
37
40
*/