Tuesday, June 3, 2008

Re: What are we aiming to accomplish with this code?

Hey Technotrons!

You still haven't been able to figure this one out yet :-)

int remove_duplicates(int * p, int size)
{
int current, insert = 1;
for (current=1; current < size; current++)
if (p[current] != p[insert-1])
{
p[insert] = p[current];
current++; insert++;
}
else current++;
return insert;
}

3 comments:

Anonymous said...

It seems to be a good attempt to remove duplicate integer numbers from consecutive memory locations but it may raise exception (Access denied) if this code attempts to access memory locations outside the address space of current program.

trg tech said...

Duplicate entries... Yes
Memory locations... No :-)

Unknown said...

It is supposed to remove duplicate entries from a sorted array of integers. However, there seems to be one thing wrong with it. Or perhaps I have just not been able to see the wisdom of it. In the assignment statement specified in the for loop

for (current=1; current < size; current++)



the 'current++' part is unnecessary, and would actually cause the first half of the array to include incorrect values e.g. values of alternate elements of original array in most cases. If we were to remove the 'current++' part, it would basically remove any duplicate entries, and the return value would specify what the new size of the (possibly) shortened array is.
Now where's my prize? :P