| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Take out dups in a list |
The question was to take out the dups in a list of integers, wasn't it?
#include
#include
#include
int compare(const void * first, const void * second)
{
int retval;
static int ifirst;
static int isecond;
ifirst = *((int *)first);
isecond = *((int *)second);
if (ifirst < isecond) retval = -1;
else if (ifirst == isecond) retval = 0;
else if (ifirst > isecond) retval = 1;
return retval;
}
#define array_size 20
void fill_the_list(int * the_list)
{
int counter, numerator = array_size / 2;
srand((unsigned)time(NULL));
for (counter = 0; counter < array_size; counter++) {
the_list[counter] = (rand() % numerator);
}
}
void take_out_dups(int * first_list, int * second_list)
{
int counter1 = 1, counter2 = 1;
int comparison = second_list[0] = first_list[0];
while (counter1 < array_size) {
if (first_list[counter1] != comparison) {
comparison = first_list[counter1];
second_list[counter2] = first_list[counter1];
counter1++;
counter2++;
} else {
counter1++;
}
}
}
int main()
{
int a_list[array_size];
int b_list[array_size] = {0};
fill_the_list(a_list);
qsort( (void *)a_list, (size_t)array_size, sizeof(int), compare);
take_out_dups(a_list, b_list);
return 0;
}
In the end, a_list is a sorted list of integers, b_list is the values of
a_list _without_ the duplicates.
þ CMPQwk 1.42 999
--- Maximus/2 3.01
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)SEEN-BY: 633/267 270 @PATH: 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.