remove double entries from a sorted array

E

ebc

Hi,

I have written a function that removes double entries from a sorted
array.

See the structures
typedef struct tagRECR
{
char name[LEN_NAME+1];

}
REC;
// the REC structure is filled dynamic (malloc...)
typedef struct tagTAB
{
int count; // count of itemes in lief
REC *lief; // dynamic generated list
// belegte anz Eintr,,ge
}
TAB;

Now the function

void removedoubleentries( TAB *tab )
{
int i,j;
RECL *source;
RECL *dest;
BOOL found;

source = tab->lief;
dest = source;

j=0;
found = FALSE;

for (i = 0; i < tab->count-1; i ++ )
{
if ( strcmp ( (source+i)->name, (source+1+i)->name) !=0 )
{
*(dest+j) = *(source+i);
j++;
found = FALSE;
}
else
found = TRUE;
}
// Now I am not sure if this is correct
// for the last entrie
*(dest+j) = *(source+i);
j++;
tab->count = j;

}

ebc
 
A

akarl

ebc said:
I have written a function that removes double entries from a sorted
array.

See the structures
typedef struct tagRECR
{
char name[LEN_NAME+1];

}
REC;
// the REC structure is filled dynamic (malloc...)
typedef struct tagTAB
{
int count; // count of itemes in lief
REC *lief; // dynamic generated list
// belegte anz Eintr,,ge
}
TAB;

Now the function

void removedoubleentries( TAB *tab )
{
int i,j;
RECL *source;
RECL *dest;
BOOL found;

source = tab->lief;
dest = source;

j=0;
found = FALSE;

for (i = 0; i < tab->count-1; i ++ )
{
if ( strcmp ( (source+i)->name, (source+1+i)->name) !=0 )
{
*(dest+j) = *(source+i);
j++;
found = FALSE;
}
else
found = TRUE;
}
// Now I am not sure if this is correct
// for the last entrie
*(dest+j) = *(source+i);
j++;
tab->count = j;

}

Why not use ordinary array notation; `dest[j] = source' etc?

August
 
M

Michael Mair

ebc said:
Hi,

I have written a function that removes double entries from a sorted
array.

Please state what you are looking for
- a code review?
- finding the solution of a problem/if yes, which problem?

See the structures
typedef struct tagRECR
{
char name[LEN_NAME+1];

}
REC;
// the REC structure is filled dynamic (malloc...)
typedef struct tagTAB
{
int count; // count of itemes in lief

I would rather use size_t instead of int -- the guaranteed upper
bound of int is 32K-1...
REC *lief; // dynamic generated list
// belegte anz Eintr,,ge
}
TAB;

Now the function

void removedoubleentries( TAB *tab )
{
int i,j;
RECL *source;
RECL *dest;

The type RECL is not defined.
BOOL found;

The type BOOL is not defined.

Please do not post as-if-code but copies of the real thing.
source = tab->lief;
dest = source;

j=0;
found = FALSE;

What do you need found for?
for (i = 0; i < tab->count-1; i ++ )
{
if ( strcmp ( (source+i)->name, (source+1+i)->name) !=0 )

If you want to use pointers, use pointers, i.e. update source in
the loop update statement by source++ and
compare source->name and source[1].name/(source+1)->name.

Otherwise, use array index access as it is easier to read:
if ( strcmp(source.name, source[i+1].name) != 0 )

If you do not have many double first letters, you can speed up this
check:
if ( source.name[0] != source[i+1].name[0]
|| strcmp(source.name, source[i+1].name) != 0 )
{
*(dest+j) = *(source+i);
j++;

with pointer arithmetics
*(dest++) = *source;
(parentheses not necessary).
Part array/part pointer:
*dest++ = source;
found = FALSE;
}
else
found = TRUE;
}
// Now I am not sure if this is correct
// for the last entrie
*(dest+j) = *(source+i);

Have you tried it?
j++;
tab->count = j;

}

Why don't you give us something that compiles and tell us
your problem?

Note: Uppercase name are traditionally used for macros -- I
would rather not use them for typedefs.


Cheers
Michael
 
B

Barry Schwarz

Hi,

I have written a function that removes double entries from a sorted
array.

See the structures
typedef struct tagRECR
{
char name[LEN_NAME+1];

}
REC;
// the REC structure is filled dynamic (malloc...)
typedef struct tagTAB
{
int count; // count of itemes in lief
REC *lief; // dynamic generated list
// belegte anz Eintr,,ge
}
TAB;

Now the function

void removedoubleentries( TAB *tab )
{
int i,j;
RECL *source;

What is a RECL?
RECL *dest;
BOOL found;

source = tab->lief;

source is a RECL*. tab->lief is a REC*. This is a syntax error
unless one of them is a synonym for void* and we know REC* is not.
Why don't you show us your real code? Use cut and paste; don't
retype.
dest = source;

j=0;
found = FALSE;

This value will survive only if tab->count <= 1.
for (i = 0; i < tab->count-1; i ++ )
{
if ( strcmp ( (source+i)->name, (source+1+i)->name) !=0 )

source.name saves a couple of keystrokes and is easier on the eyes.
{
*(dest+j) = *(source+i);

As is dest[j].
j++;
found = FALSE;

What is found used for?
}
else
found = TRUE;
}
// Now I am not sure if this is correct
// for the last entrie
*(dest+j) = *(source+i);
j++;

Only if the above copy is performed. Maybe you should be using found
here. But watch out if tab->count is 0.
tab->count = j;

}

ebc



<<Remove the del for email>>
 
E

ebc

Thanks for reply all of you..

I have not just copy the original code because the variables are in
german. So for better reading i give you the code "translated" to
english.

First of all I' am looking if this could be work. I can not test it
yet.

-RECL is a syntax error it should be REC. -> re typing error :)

<I would rather use size_t instead of int -- the guaranteed upper
bound of int is 32K-1... >
--> int is okay this array has a limit to 100 items. ( makes another
part of my program )

@Barry Schwarz and all others

I give you the original code

typedef struct tagRECLIEFNR
{
char vertrag[LEN_SAP_AUFTRAG+1];
char lieferscheinnr[LEN_SAP_LSCHEIN+1];
char liefText[LEN_SAP_LIEFTXT+1];
int stk;
}
RECLIEFNR;
typedef struct tagTABLIEFNR
{
int anz; // = counts of lief
int size; // Gr"áe Feld lief
RECLIEFNR *lief; // dynamic Filed with size size and count anz
}
TABLIEFNR;
static void RemoveDoppelteLSEintraege( TABLIEFNR *tsource )
{
int i,j;
RECLIEFNR *source;
RECLIEFNR *dest;

source = tsource->lief;
dest = source;

j=0;
i=0;
//found = FALSE;

if ( tsource->anz-1 < 0 )
return;

for (i = 0; i < tsource->anz-1; i ++ )
{
// no double entrie
if ( strcmp ( (source+i)->lieferscheinnr,
(source+1+i)->lieferscheinnr) != 0 )
{
*(dest+j) = *(source+i);
j++;
//found = FALSE;
}

}
// for the last entrie
*(dest+j) = *(source+i);
j++;
tsource->anz = j;

}
The problem with the anz < 1 should be now no problem.

Will this code work ?

And i know that this style *(dest+j) is'nt the best. I will change it
when i have time.

Thanks a lot

ebc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top