remove double entries from a sorted array

Discussion in 'C Programming' started by ebc, Aug 2, 2005.

  1. ebc

    ebc Guest

    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
     
    ebc, Aug 2, 2005
    #1
    1. Advertising

  2. ebc

    akarl Guest

    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' etc?

    August
     
    akarl, Aug 2, 2005
    #2
    1. Advertising

  3. ebc

    Michael Mair Guest

    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.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
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Aug 2, 2005
    #3
  4. ebc

    Guest

    cannot catch wot you mean
     
    , Aug 3, 2005
    #4
  5. In article <>,
    <> wrote:
    >cannot catch wot you mean
    >


    ITYM:

    cannut ketch wt u mn
     
    Kenny McCormack, Aug 3, 2005
    #5
  6. 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.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>>
     
    Barry Schwarz, Aug 3, 2005
    #6
  7. ebc

    ebc Guest

    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
     
    ebc, Aug 3, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,597
    Victor Bazarov
    Feb 17, 2005
  2. Replies:
    1
    Views:
    394
    Mark P
    Mar 25, 2006
  3. Replies:
    5
    Views:
    449
    James Kanze
    Jun 27, 2008
  4. Don Bruder
    Replies:
    3
    Views:
    1,012
    spikeysnack
    Aug 3, 2010
  5. Sean O'Halpin
    Replies:
    2
    Views:
    106
    Dave Burt
    Sep 28, 2005
Loading...

Share This Page