c program for sort and remove duplicate values from an array

This is a discussion on c program for sort and remove duplicate values from an array within the C Programming forums, part of the General Programming Boards category; Hi , I have a string array having a bunch of values. I need to sort this array, remove duplicate ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    c program for sort and remove duplicate values from an array

    Hi ,

    I have a string array having a bunch of values. I need to sort this array, remove duplicate values from this array and reset the filtered array (without duplicate values) to a new array.

    I am new to C programming. I have written some code, but it fails with a core dump always. Can some one please correct my code or send me an easy way to do this.

    Thanks.
    Code:
    code snippet:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    main ()
    {
    int stat = 0;
    int ctr = 4;
    int inx = 0;
    int status = 0;
    int j = 0;
    int k = 0;
    int reset_count = 0;
    char **lov_reset_values = 0;
    char *str[] = { "test", "test1", "test2", "test" };
    
    for ( inx = 0; inx < ctr; inx ++ )
    {
    printf ( "str[%d] = %s\n", inx, str[inx] );
    }
    
    for(j=0;j<ctr;j++)
    {
    status = 0;
    for(k=1;k<=ctr;k++)
    {
    printf ( "entered after k= 0\n" );
    if( strcmp (str[j],str[k] ) == 0 )
    {
    printf ( "matched\n" );
    status = 1;
    break;
    }
    }
    if ( status == 0 )
    {
    if (reset_count == 0 )
    {
    printf ( "reset_count = 0\n" );
    lov_reset_values = (char **)malloc( sizeof(char *));
    }
    else
    {
    printf ( "reset_count = %d\n", reset_count );
    lov_reset_values = (char **)realloc( lov_reset_values, sizeof(char *)*(reset_count+1) );
    }
    
    lov_reset_values[reset_count] = (char *)malloc( sizeof( char )*(strlen(str[j])+1));
    strcpy ( lov_reset_values[reset_count], (str)[j] );
    printf ( "lov_reset_values[%d] = %s\n", reset_count, lov_reset_values[reset_count]);
    reset_count = reset_count + 1;
    }
    }
    
    printf (" reset_count = %d\n", reset_count );
    
    /*
    for ( inx = 0; inx < reset_count; inx++ )
    {
    printf ( "lov_reset_values[%d] = %s\n", inx, lov_reset_values[inx] );
    }
    */
    
    return stat;
    }
    Last edited by Salem; 07-28-2009 at 02:08 PM. Reason: Added [code][/code] tags, learn to do it yourself

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    Added code tags, but didn't help as the code lacks any indentation at all.
    This needs to be fixed ASAP.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Here is the updated code with the indentation:
    Here is the code with proper indentation:


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>

    main ()
    {
    int stat = 0;
    int ctr = 4;
    int inx = 0;
    int status = 0;
    int j = 0;
    int k = 0;
    int reset_count = 0;
    char **lov_reset_values = NULL;
    char *str[] = { "test", "test1", "test2", "test" };

    for(j=0;j<ctr;j++)
    {
    status = 0;
    for(k=1;k<=ctr;k++)
    {
    if( strcmp (str[j],str[k] ) == 0 )
    {
    printf ( "matched\n" );
    status = 1;
    break;
    }
    }

    if ( status == 0 )
    {
    if (reset_count == 0 )
    {
    printf ( "reset_count = 0\n" );
    lov_reset_values = (char **)malloc( sizeof(char *));
    }
    else
    {
    printf ( "reset_count = %d\n", reset_count );
    lov_reset_values = (char **)realloc( lov_reset_values,
    sizeof(char *)*(reset_count+1) );
    }

    lov_reset_values[reset_count] = (char *)malloc( sizeof( char )*(strlen(str[j])+1));
    strcpy ( lov_reset_values[reset_count], (str)[j] );
    printf ( "lov_reset_values[%d] = %s\n", reset_count, lov_reset_values[reset_count]);
    reset_count = reset_count + 1;
    }
    }

    printf (" reset_count = %d\n", reset_count );
    return stat;
    }

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    4
    Sorry, attached the file with the right indentation. Any help in fixing this issue of sorting and removing the duplicate entries from an existing string array and re-writing the filtered list to a different array, is appreciated.

    Thanks.
    Attached Files Attached Files
    • File Type: c ta.c (1.6 KB, 143 views)

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First of all, use code tags (and be sure that your code is indented). Next, the easiest way to do this is:

    Code:
    1) create two arrays: 'a2' and 'a3'
    2) copy the original array to 'a2'
    3) qsort 'a2'
    4) create a char*: 'last', set to an empty string (eg: "")
    5) create two char**: 'c2' and 'c3' and set them to 'a2' and 'a3', respectively
    6) loop
        6b) while the the string at 'c2' equals 'last' (use strcmp), increment 'c2'
        6c) copy the string at 'c2' to 'c3'
        6d) set 'last' to the string at 'c2'
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Also I do not believe that you will ever check that status == 0? I would place some printf statements imediately after your inner loop. Check what values j and k are afterwards. Also your seg fault is do to you overstepping your array bounds with crt. Set k = 0 in your for loop, and have your for conditional check if the values or actually less than crt not less-than or equal-to.
    Last edited by slingerland3g; 07-28-2009 at 03:25 PM.

  7. #7
    Registered User Cooloorful's Avatar
    Join Date
    Feb 2009
    Posts
    59
    Edit: Ooops! my bad.
    wipe on -
    A slap on the hand is better than a slap on the face. A tragic lesson learned far too late in life.
    - wipe off

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Try coming up with an algorithm to solve this problem before diving into the coding process.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 03:03 PM
  5. How do I remove duplicate entries in an array?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-18-2002, 09:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21