Thread: Bubble Sort String Comparison

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    3

    Bubble Sort String Comparison

    Hi All,

    I've been working on this bit of code for a week now - I think I'm close to a solution, but for some reason my program keeps crashing - I've checked several forums including this one for solutions, but I guess I'm missing the basics of constant pointers or something.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 12
    
    int main(void)
    {
        FILE* handle1 ;
        FILE* handle2 ;
        FILE* handle3 ;
        int i, j ;
        const char nameA[ MAX ][ MAX ] ;
        const char temp[ MAX ][ MAX ] ;
    
        handle1 = fopen ( "boys.txt", "r" ) ;                        // open file for reading
        handle2 = fopen ( "girls.txt", "r" ) ;                      // open file for reading
        while ( !feof( handle1 ) )                                  // read the data from boys.txt (file handle1) into array A
        {
            fscanf( handle1,"%s", &nameA[ i ] ) ;
            printf( "%d %s\n", i, nameA[ i ] ) ;
            i++ ;
        }
        fclose( handle1 ) ;
        while ( !feof( handle2 ) )                                  // read & append the data from girls.txt (file handle2) into array A
        {
            fscanf( handle2,"%s", &nameA[ i ] ) ;
            printf( "%d %s\n", i, nameA[ i ] ) ;
            i++ ;
        }
        fclose( handle2 ) ;
        for ( i = 0; i < MAX-1; i++ )
        {
            for (j = 0; j < MAX-1; j++ )
            {                                                       // if names compared are not the same and next name is greater than nameA
                if ( strcmp( nameA[ j ], nameA[ j+1 ] ) > 0 )
                 {
                    printf("%d %s compared to %s", i, nameA[j], nameA[j+1] ) ;
                    temp = nameA[ j ] ;
                    nameA[ j ] = nameA[ j+1 ] ;                     // swap places with the previous string index in array A
                    nameA[ j+1 ] = temp ;
                }
    
            }
    I'm kinda new to most of this - so, any comments about bad code are appreciated

    I commented out most of the code & was able to get the program to work up until the strcmp bit. To me, the code:
    Code:
    if ( strcmp( nameA[ j ], nameA[ j+1 ] ) > 0 )
    makes sense and i've tried using the same with
    Code:
    if ( !strcmp( nameA[ j ], nameA[ j+1 ] ) )
    , but I'm not sure if it's something to do with the array not being const or not const. I've tried many different combinations, but to be honest I'm not sure exactly what I'm doing when using const and * pointer together with [] an array.
    I do know that:
    const = constant
    a pointer points to a place in memory = *
    and an array [] is also considered a pointer to a place in memory
    right?
    Last edited by Spare_tyre; 05-03-2012 at 09:53 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since you intend to modify their elements, these arrays:
    Code:
    const char nameA[ MAX ][ MAX ] ;
    const char temp[ MAX ][ MAX ] ;
    should have been declared as:
    Code:
    char nameA[MAX][MAX];
    char temp[MAX][MAX];
    Do not use feof to control a loop like that. Rather, use the return value of fscanf. Furthermore, nameA[i] is an array of char, so you should just use it as an argument to fscanf, not &nameA[i].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    3
    Thank you for the feedback Laserlight (much appreciated! )
    - I'll give this a go now!

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    3
    I tried this and I'm still having problems. I substituted the while loop like you suggested for something from the book:
    Code:
    while (EOF != fscanf(handle2, "%s",nameA[ i ] ) )           // read the data from boys.txt (file handle1) into array A
        {
            printf( "%d %s\n", i, nameA[ i ] ) ;
            i++ ;
        }
        fclose( handle2 ) ;
    however, the main problem still lies in the swapping of strings.
    I'm not sure if the temp varaible should be an int or a char, but the error i'm getting keeps saying something along these lines:
    " incompatible types when assigning to type 'char[12][12]' from type 'char *' "
    " incompatible types when assigning to type 'char[12]' from type 'char *' "
    " incompatible types when assigning to type 'char[12]' from type 'char (*) [12]' "

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubble Sort Using C-Style String
    By Sammy2011 in forum C++ Programming
    Replies: 7
    Last Post: 11-18-2011, 08:29 PM
  2. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  3. using bubble sort to sort a matrix by sum..
    By transgalactic2 in forum C Programming
    Replies: 22
    Last Post: 12-23-2008, 12:03 AM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  5. Sorting a string using bubble sort?!
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 05:44 PM