Sorting string array

This is a discussion on Sorting string array within the C++ Programming forums, part of the General Programming Boards category; Code: for (int i = 0; i < (BOOK_MAX); ++i) { Sort[i] = Books[i].Author; } sort(Sort, Sort+BOOK_MAX); cout << Sort[1] ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    66

    Sorting string array

    Code:
            for (int i = 0; i < (BOOK_MAX); ++i)
             {
               Sort[i] = Books[i].Author;
             }
            sort(Sort, Sort+BOOK_MAX);
            cout << Sort[1] << Sort[2] << Sort[3] << endl;
    I don't receive any errors at compile, but cout produces lots of empty spaces for some reason without the text I want. I tested with cout and Books[i].Author is not empty.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, how many books do you actually have, and what is BOOK_MAX?

    An empty string is "smaller" than all other strings, so if you have
    Code:
    "Bjarne Stroustrup"
    "Scott Meyers"
    "Barbara Moo"
    ""
    ""
    ""
    ""
    then the sorted list would be:
    Code:
    ""
    ""
    ""
    ""
    "Barbara Moo"
    "Bjarne Stroustrup"
    "Scott Meyers"
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    BOOK_MAX is 2000, I have 28 books. However, I don't get any lines of text at all and the spaces don't look anywhere close to 2000.

    I updated the code, and it still gives me lots of blanks.
    Code:
    for (int i = 0; i < (BOOK_MAX); ++i)
             {
               if (Books[i].Author == "")
                 {
                 }
               else
                 {
               Sort[i] = Books[i].Author;
                 }
             }
            sort(Sort, Sort+BOOK_MAX);
            cout << Sort[1] << Sort[2] << Sort[3] << endl;
    Last edited by Furious5k; 01-06-2009 at 09:03 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Furious5k View Post
    BOOK_MAX is 2000, I have 28 books. However, I don't get any lines of text at all and the spaces don't look anywhere close to 2000.

    I updated the code, and it still gives me lots of blanks.
    Code:
    for (int i = 0; i < (BOOK_MAX); ++i)
             {
               if (Books[i].Author == "")
                 {
                 }
               else
                 {
               Sort[i] = Books[i].Author;
                 }
             }
            sort(Sort, Sort+BOOK_MAX);
            cout << Sort[1] << Sort[2] << Sort[3] << endl;
    So what does sort[i] contain if Books[i].Auther is ""?

    I think you need two index variables, one for the sort array, and one for the books array, and then only increment the second index when the author is non-blank - then only sort the items you filled in [the second index variable will have that value].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    I need to learn to read, I was still sorting the the whole of the array.
    Last edited by Furious5k; 01-06-2009 at 09:26 AM.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    I am now trying to eliminate duplicates from the list before sorting it and after adding the code again getting blanks.
    Code:
             int flag = 0;
             int count = 0;
             for (int i = 0; i < (BOOK_MAX); ++i)
               {
                 if (Books[i].Author == "")
                   {
                   }
                 else
                   {
                     for (int x = 0; x < count; ++x)
                       {
                         if (Books[i].Author == Sort[x])
                           {
                             ++flag;
                           }
                       }
                     if (flag = 0)
                       {
                         Sort[count] = Books[i].Author;
                         ++count;
                       }
                     flag = 0;
                   }
               }
            sort(Sort, Sort+count);
    Last edited by Furious5k; 01-06-2009 at 09:40 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's probably better to sort the books first, then copy each unique author to another list - an author is unique if the previous author is different from the current one. After all, that only requires ONE iteration through the list of authors, not n^2 iterations like your current implementation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is much easier to remove duplicates after sorting. In an unsorted array the duplicate could be anywhere. In a sorted array the duplicates are all nicely next to each other.

    In fact there is another algorithm for removing duplicates from a sorted container.

    Code:
    #include <iostream>
    #include <algorithm>
    
    int main()
    {
        int arr[] = { 2, 1, 6, 4, 22, 4, 2, 1, 3 };
        const unsigned max = sizeof(arr) / sizeof(arr[0]);
    
        std::sort(arr, arr + max);
    
        //std::unique only works if the array is sorted
        const unsigned max_unique = std::unique(arr, arr + max) - arr;
    
        for (unsigned i = 0; i != max_unique; ++i) {
            std::cout << arr[i] << ' ';
        }
    
        //items in range max_unique...max are not valid
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Code:
            int uniquecount;
            for (int x = 0; x < count; ++x)
              {
                if (Sort[x] == Sort[x+1])
                  {
                  }
                else
                  {
                    Unique[uniquecount] = Sort[x];
                    ++uniquecount;
                  }
              }
    Urgh, segmentation fault. It seems perfectly fine and I'm sure it's something obvious, again...

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    Sort[x+1]
    will access element number Count, which may not be defined, perhaps?

    Or
    Code:
            int uniquecount;
    is never assigned a value, so it has some arbitrary (possibly LARGE) value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    uniquecount = 0; worked.

    It still seems that the [x+1] would be accessing an array location that does not exist. But if it works, it doesn't matter, I guess, since it wouldn't cause wrong output.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Furious5k View Post
    uniquecount = 0; worked.

    It still seems that the [x+1] would be accessing an array location that does not exist. But if it works, it doesn't matter, I guess, since it wouldn't cause wrong output.
    Well, instead of looking at the next element, perhaps you should stuff the first element into the Unique array, and then compare with the latest element in the Unique array to see if you need to add it or not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Code:
    for (int q = 0; q < uniquecount; ++q)
              {
                for (int w = 0; w < BOOK_MAX; ++w)
                  {
                    if (Unique[q] == Books[w].Author)
                      {
                        cout << Books[w].Title << " in section " << Books[w].Category;
    
                        for (int e = 0; e < BOOK_MAX; ++e)
                          {
                            if (Book[w].ID == Loans[e].Book)
                              {
                                cout << " - on Loan." << endl;
                              }
                            else
                              {
                                cout << " - in Stock." << endl;
                              }
                          }
                      }
                  }
              }
    198: parse error before `['
    213: break statement not within loop or switch
    214: confused by earlier errors, bailing out

    Line 198 is the if statement.
    I'm confused about break statement, because the switch statement it's in is within braces.
    The switch starts and ends as follows:
    Code:
    switch (select)
         {
         case 1:
           {
    Code:
            break;
           }
         case 2:
    I didn't have any errors before writing the code that starts at the point which I've pasted.
    Last edited by Furious5k; 01-06-2009 at 10:32 AM.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably have an error elsewhere - what you have posted seems OK to me.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    I updated the code with everything up to the closing of the first bracket. It definately has to be something there, because if I wrap comment around that code it compiles.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting 2D string array
    By sureshhewa in forum C Programming
    Replies: 14
    Last Post: 07-27-2008, 02:30 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 07:30 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM

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