| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Take out dups in a list |
Hi Neil.
05-Jun-04 22:32:00, Neil Heller wrote to All
NH> The question was to take out the dups in a list of integers, wasn't it?
it said from an array, which is what you've done...
NH> #include
NH> #include
NH> #include
NH> int compare(const void * first, const void * second)
NH> {
NH> int retval;
NH> static int ifirst;
NH> static int isecond;
why "static"? if you want speed use "register", you can
delcare both on
the same line ...
NH> ifirst = *((int *)first);
NH> isecond = *((int *)second);
... and do the assignment.
register int ifirst = *(int *)first , isecond = *(int *)second ;
NH> if (ifirst < isecond) retval = -1;
you can do.
if(ifirst < isecond) return -1;
etc and not need retval.
NH> else if (ifirst == isecond) retval = 0;
NH> else if (ifirst > isecond) retval = 1;
that last if is redundant, if I wanted to note that I'd have dropped the if
and nade the condition part a comment.
/* (ifirst > isecond) */ return 1;
NH> return retval;
NH> }
NH> #define array_size 20
NH> void fill_the_list(int * the_list)
NH> {
NH> int counter, numerator = array_size / 2;
NH> srand((unsigned)time(NULL));
NH> for (counter = 0; counter < array_size; counter++) {
NH> the_list[counter] = (rand() % numerator);
NH> }
NH> }
That's a good way to ensure plenty of duplicates. :-)
NH> void take_out_dups(int * first_list, int * second_list)
you could do declare first_list as "const int *" as this code doesn't
modify it, (it's details like that that I often forget, and not-critical
in proof of concept code like this.
you should also pass the size of the array, if the function is to be of
general use. I note that the function works in the case where
firstlist==secondlist (which is good).
NH> {
NH> int counter1 = 1, counter2 = 1;
NH> int comparison = second_list[0] = first_list[0];
NH> while (counter1 < array_size) {
NH> if (first_list[counter1] != comparison) {
NH> comparison = first_list[counter1];
NH> second_list[counter2] = first_list[counter1];
NH> counter1++;
NH> counter2++;
NH> } else {
NH> counter1++;
NH> }
NH> }
NH> }
NH>
the only problem I see is there's no anouncement of the number of unique
valies found in the list...
immangine the first array had {-1;-1;0;0} in it.
the second array gets {-1;0;0;0;}
(becaue you initialise it to all 0 in main)
but it's not immediately obvious wither the second element is a number form
the first list or marks the end...
also announcing the number of unique values (counter 2 in your code)
makes it actually usable when passed the same array for input and output...
the easiest way to do that is to change the return-type of the function to
"int" (or size_t) and put "return counter2;" at the
bottom of the function
-=> Bye <=-
---
* Origin: Success is a journey, not a destination. (3:640/1042)SEEN-BY: 633/267 270 @PATH: 640/1042 531 954 774/605 123/500 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™.