Thread: Problem sorting an array of strings

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    103

    Problem sorting an array of strings

    Hi!!
    Can anybody tell me what wrong with this code, it is supposed to classify the strings alphabetically and display them after...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 24
    #define STR_NUMBER 5
    
    int main(){
               char str[STR_NUMBER][SIZE];
               char temp[SIZE];
               int i,j;
               for (i = 0 ; i < STR_NUMBER ; i++)
               gets(str[i]);
               int n = 0;
               for (j = 0 ; j < STR_NUMBER - 1 ; j++){
                   n = strcmp(str[j],str[j+1]);
                   if (n>0){
                   strcpy(temp,str[j]);
                   strcpy(str[j],str[j+1]);
                   strcpy(str[j+1],temp);
                   }
                   }
                   
               printf("\n\nthe array of %d strings sorted:\n\n",STR_NUMBER);
                for (int i = 0 ; i < STR_NUMBER ; i++)
                    puts(str[i]);
               system("PAUSE");
               return(0);
    Simple input:
    a
    r
    v
    g
    t
    Output:
    a
    r
    g
    t
    v

    Which is totally wrong!!

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Sorry but i guess J should go up to STR_NUMBER not included so
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 24
    #define STR_NUMBER 5
    
    int main(){
               char str[STR_NUMBER][SIZE];
               char temp[SIZE];
               int i,j;
               for (i = 0 ; i < STR_NUMBER ; i++)
               gets(str[i]);
               int n = 0;
               for (j = 0 ; j < STR_NUMBER; j++){
                   n = strcmp(str[j],str[j+1]);
                   if (n>0){
                   strcpy(temp,str[j]);
                   strcpy(str[j],str[j+1]);
                   strcpy(str[j+1],temp);
                   }
                   }
                   
               printf("\n\nthe array of %d strings sorted:\n\n",STR_NUMBER);
                for (int i = 0 ; i < STR_NUMBER ; i++)
                    puts(str[i]);
               system("PAUSE");
               return(0);         
    }
    Still, there is a mistake..

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Anyone

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define SIZE 1024
    
    void Sort(char str[][SIZE], size_t strCount);
    int main(int argc, char* argv[])
    {
        char strList[][SIZE] = {"ZZZZZ", "BBB", "AAA", "MMMM"};
    
        Sort(strList, 4);
    
        for(int i = 0; i < 4; ++i)
            printf("&#37;s\n", strList[i]);
        return 0;
    }
    
    //Simple selection sort
    void Sort(char str[][SIZE], size_t strCount)
    {
        //Loop through all the elements
        for(unsigned int i = 0; i < strCount; ++i)
        {
            //Set smallest to current element
            char* smallest = str[i];
            
            //Get the smallest string (starting after current element)
            for(unsigned int ii = i + 1; ii < strCount; ++ii)
            {
                if(strcmp(str[ii], smallest) < 0)
                    smallest = str[ii];
            }
    
            char temp[sizeof(*str)];
            //Copy smallest into temp
            strncpy(temp, smallest, sizeof(*str));
            //Swap current element into the smallest
            strncpy(smallest, str[i], sizeof(*str));
            //Swap the temp into the current element
            strncpy(str[i], temp, sizeof(*str));
        }
    }
    Last edited by 39ster; 05-07-2008 at 08:51 PM.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    What was that size_t...?? I just know the commonly used data types, we didn't cover typedef or structures if it is the case here!!
    Probably not...And my question was what wrong with my code, copying that code below is not very instructive!!

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by Leojeen View Post
    What was that size_t...?? I just know the commonly used data types, we didn't cover typedef or structures if it is the case here!!
    Probably not...And my question was what wrong with my code, copying that code below is not very instructive!!
    I've added many comments for you to read in the sorting algorithm. size_t is a typedef of "unsigned int" and is defined in the standard c headers so you can just replace "size_t" with "unsigned int" if you want. No one is going to give you a better answer than a fully commented working sorting function. Just compare mine to yours.
    Last edited by 39ster; 05-07-2008 at 08:54 PM.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok, good then, thanks

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    To be pedantic (which is always fun, isn't it?), size_t is not necessarily an unsigned int. It's an unsigned integer type, possibly of a different size than int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-18-2009, 05:01 PM
  2. problem with array of strings
    By cole in forum C++ Programming
    Replies: 11
    Last Post: 09-28-2008, 11:45 PM
  3. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  4. array of strings problem
    By dayknight in forum C Programming
    Replies: 3
    Last Post: 11-08-2005, 10:41 AM
  5. Replies: 6
    Last Post: 10-21-2003, 09:57 PM