Thread: Help with sorting

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    6

    Help with sorting

    I need help sorting a list of names that the user enters..
    the strcmp routine is tricky..
    are there any sites that would prove to be helpful

    #include<iostream.h>
    #include<string.h>

    void SortApplicant( const char*[]);

    main()
    {
    char sentence[80];

    for ( int i = 0; i < 10; i++ )

    cin.getline(sentence, 40);




    return 0;
    }



    void SortApplicants(char *alpha[])
    {

    char temp;
    int pass, k, row;

    for(pass = 0; pass < 10; pass++)
    { for(k = pass; k >=1; k--)
    { if(strcmp(alpha[k], alpha[k-1]) > 0)
    { temp = alpha[k];
    alpha[k] = alpha[k-1];
    alpha[k-1] = temp;
    }
    }
    }




    for(row = 0; row < 5; row ++)
    { printf("%s", alpha[row]);
    printf("\n");
    }

    return;}


    practive makes perfect huh

  2. #2
    Unregistered
    Guest
    you're off a little bit in several places. It looks like you need to be a little more precise in your thinking although the generalities seem ok.

    let's start here:

    if(strcmp(alpha[k], alpha[k-1]) > 0)

    in order for this to work alpha[k] and alpha[k - 1] both need to be strings. Now look at this:

    temp = alpha[k];

    There are actually two errors here. First alpha[k] is a string as per the first statement above. However, you cannot assign one string to another (at least not a null terminated char array which is the type of string alpha[k] is). The second error is that temp is declared as a single char, not a string at all. Therefore you can't assign a string to a single char, either. You should redeclare temp to be a string and then strcpy() alpha[k] into it. Ditto throughout the rest of the swapping sequence.

    Noiw look at these two lines.

    for(pass = 0; pass < 10; pass++)
    { for(k = pass; k >=1; k--)


    Write out what they say in your native language to find the error yourself. Hint: it's a logic error, not a syntax error.

    Next SortApplicant is different to the compiler than SortApplicants. Be sure you always use the same name for variables and functions throughout the program.

    Last, you need to write the rest of the code. You never call SortApplicant in main() and you need to parse the users input if they enter all data at once rather than one word/name at a time.

    Not the least, practice doesn't make perfect. practice makes better. There is no such thing as perfection.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    6

    ThankU

    THank You...looks like a need a lot of practice with this c++ stuff..

  4. #4
    Unregistered
    Guest
    we all do(did????)

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    6

    i wonder if there's an easier way to do this

    Is there an easier way to do this...

  6. #6
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Why don't you just do a bubble sort, and write a swap function to swap the names accordingly.
    Be a leader and not a follower.

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    6
    Can u help me with that

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    'char temp' should be 'char *temp'
    everything else should work... as far as SortApplicants is concerned
    Last edited by *ClownPimp*; 07-12-2002 at 10:02 PM.

  9. #9
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    this should be ok.

    Code:
    #include <iostream.h>
    
    const int NUM_NAMES=5, NAME_LEN=30;
    
    void Swap(char [], char[]);
    void DisplayArray(const char Array[NUM_NAMES][NAME_LEN]);
    
    int main()
    {
    
    
      char Array[NUM_NAMES][NAME_LEN]={{"ef"}, {"de"},
                                       {"cd"}, {"bc"},
                                       {"ab"}
                                      }, Temp[NAME_LEN];
    
      cout << "Array before sort: \n\n";
    
      DisplayArray(Array);
    
      for(int Names=NUM_NAMES-1; Names >= 0; Names--)
        for(int Name=0; Name < Names; Name++)
          if(strcmp(Array[Name], Array[Name+1])>0)
            Swap(Array[Name], Array[Name+1]);
    
      cout << "\nArray after sort: \n\n";
    
      DisplayArray(Array);
    
      getchar();
    
      return 0;
    }
    
    void Swap(char String1[], char String2[])
    {
      static char Temp[NAME_LEN];
    
      strcpy(Temp, String2);
      strcpy(String2, String1);
      strcpy(String1, Temp);
    }
    
    void DisplayArray(const char Array[NUM_NAMES][NAME_LEN])
    {
      for(int i=0; i < NUM_NAMES; i++)
        cout << Array[i] << "\n";
    }
    Be a leader and not a follower.

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    when you have an array of char* as aba had, its not necessary to strcpy the values, just swap the pointers

  11. #11
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    I'm sure he can modify it for his own needs, just showing the general process of a bubble sort. Hadn't heard of it before.
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  3. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 08:05 PM