Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > remove double entries from a sorted array

Reply
Thread Tools

remove double entries from a sorted array

 
 
ebc
Guest
Posts: n/a
 
      08-02-2005
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

 
Reply With Quote
 
 
 
 
akarl
Guest
Posts: n/a
 
      08-02-2005
ebc wrote:
> 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[i]' etc?

August
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      08-02-2005
ebc wrote:
> 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[i].name, source[i+1].name) != 0 )

If you do not have many double first letters, you can speed up this
check:
if ( source[i].name[0] != source[i+1].name[0]
|| strcmp(source[i].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[i];

> 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
uniquedoggie@gmail.com
Guest
Posts: n/a
 
      08-03-2005
cannot catch wot you mean

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      08-03-2005
In article <. com>,
<> wrote:
>cannot catch wot you mean
>


ITYM:

cannut ketch wt u mn

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      08-03-2005
On 2 Aug 2005 07:43:47 -0700, "ebc" <> wrote:

>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[i].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>>
 
Reply With Quote
 
ebc
Guest
Posts: n/a
 
      08-03-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Picking X random entries from linked list of Y entries Don Bruder C Programming 3 08-03-2010 09:10 AM
is it safe to access a complex<double> array as a double array oftwice the length? huili80@gmail.com C++ 5 06-27-2008 11:13 AM
Remove entries from add remove programs in win64 Kue2 Windows 64bit 22 01-25-2007 10:36 AM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Tying up Port Login table entries with Port Table Entries in CISCO SNMP John Ramsden Cisco 0 07-24-2004 04:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57