| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Take out dups in a list |
NH> The question was to take out the dups in a list of integers,
NH> wasn't it?
JB> it said from an array, which is what you've done...
Silly me; I thought a list and an array were the same thing.
JB> NH> #include
JB> NH> #include
JB> NH> #include
JB> NH> int compare(const void * first, const void * second)
JB> NH> {
JB> NH> int retval;
JB> NH> static int ifirst;
JB> NH> static int isecond;
JB>why "static"? if you want speed use "register",
you can delcare both
JB>on the same line ...
I used static in order to instantiate the variables in the global
section thereby avoiding creating space on the stack every iteration
(like the old saying: "Every little bit helps").
JB> NH> ifirst = *((int *)first);
JB> NH> isecond = *((int *)second);
JB>... and do the assignment.
JB>register int ifirst = *(int *)first , isecond = *(int *)second ;
JB> NH> if (ifirst < isecond) retval = -1;
JB>you can do.
JB> if(ifirst < isecond) return -1;
JB>etc and not need retval.
JB> NH> else if (ifirst == isecond) retval = 0;
JB> NH> else if (ifirst > isecond) retval = 1;
JB>that last if is redundant, if I wanted to note that I'd have dropped
JB>the if and nade the condition part a comment.
JB> /* (ifirst > isecond) */ return 1;
JB> NH> return retval;
JB> NH> }
JB> NH> #define array_size 20
JB> NH> void fill_the_list(int * the_list)
JB> NH> {
JB> NH> int counter, numerator = array_size / 2;
JB> NH> srand((unsigned)time(NULL));
JB> NH> for (counter = 0; counter < array_size; counter++) {
JB> NH> the_list[counter] = (rand() % numerator);
JB> NH> }
JB> NH> }
JB> That's a good way to ensure plenty of duplicates. :-)
That was the idea. However, it was misnamed: it would have been
accurate to name it "denominator".
JB> NH> void take_out_dups(int * first_list, int * second_list)
JB>you could do declare first_list as "const int *" as this code doesn't
JB>modify it, (it's details like that that I often forget, and
JB>not-critical in proof of concept code like this.
I've always considered "const" in a declaration like this more of a
pleasantry (documentation of a sort for the maintenance programmer) than
anything else. Is there ever a time when that "const" part of the
declaration IS critical?
JB>you should also pass the size of the array, if the function is to be
JB>of general use. I note that the function works in the case where
JB>firstlist==secondlist (which is good).
Actually, one list could have been used with non-dupes used to form a
linked list. In any case, the size of the second list will never exceed
the size of the first list.
JB> NH> {
JB> NH> int counter1 = 1, counter2 = 1;
JB> NH> int comparison = second_list[0] = first_list[0];
JB> NH> while (counter1 < array_size) {
JB> NH> if (first_list[counter1] != comparison) {
JB> NH> comparison = first_list[counter1];
(Continued next message...)
þ 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™.