Thread: Sorting Strings

  1. #31
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    //old headers
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    Code:
    //new headers
    #include <iostream>
    #include <cstdlib>
    //use the std namespace
    using namspace std;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #32
    Registered User
    Join Date
    May 2003
    Posts
    43
    I said cstdlib.h can't be used...

    And Cat, if there were multiple problems, why did you only tell me of the one? I fixed the Index thing, and it says that before the sort it is "5" and after it is "8, 8, 35, 92, 19" instead of "8, 12, 19, 35, 92." Also, if both of those swap functions work fine, why do they both generate this error:
    "'swap' : local function definitions are illegal?"
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  3. #33
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    I only noticed the swap at first (which is a big problem).

    The duplication is because the swap function is still broken.

    They both DO work fine, but perhaps you put the function definition in the wrong place. This was my working code:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    
    using namespace std;
    
    inline void swap(int &a, int &b){
       int temp = a;
       a = b;
       b = temp;
    }
    
    #define INDEX 5
    
    void bubble_srt(int a[], int n);
    
    int main(void)
    {
         
         int array[INDEX] = {12, 8, 35, 92, 19}; //Altered the numbers...
    
         cout<<"Before sorting:"<< endl;
         for(int i=0; i<INDEX; i++)
    		cout<< array[i]<< endl;
    
         bubble_srt(array, INDEX);
    
         cout<<"After the sort:"<< endl;
         for(int i=0; i<INDEX; i++)
    		cout<< array[i]<< endl;
    
    }
    
    void bubble_srt(int a[], int n)
    {
    	int i, j;
    	for (i=0; i<n-2; i++) {
    		for (j=0; j<n-1-i; j++){
    			if (a[j+1] < a[j])
    				swap(a[j],a[j+1]);
    		}
    	}
    }

  4. #34
    Registered User
    Join Date
    May 2003
    Posts
    43
    Hey, that actually worked... Now I've gotta get it on my laptop @_@ Anyway, thanks.
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  5. #35
    Registered User
    Join Date
    May 2003
    Posts
    43
    Ok, I haven't copied that completely out onto my laptop, but for some reason cstdlib and cstdio aren't on there, but they're on this computer. So, I don't think that will work on it. I'll give it a try now, though. I'll let you know how it works (or doesn't :-P)
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  6. #36
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Just out of curiousity...why don't you just use the std::sort( ) function (from <algorithm>) to do this like I showed in a previous post?

    Is that really more efficent, though? It still requires three passes through an ALU, and none of the operations can happen in parallel.
    Well, it's (very slightly) more memory efficient. I dunno, I just don't like decalring variables that I don't have to. I threw it in there because I thought it was kind of a cool tidbit...and I like being crafty with booleans. As far as speed effeciency, yeah there probably is no noticable difference, although I've never tested it.

  7. #37
    Registered User
    Join Date
    May 2003
    Posts
    43
    I will try it, I was just using the first one thrown at me to get it to work. And, hey, it did. Its interesting though... I dropped the .h from the end of cstdlib and cstdio and it worked. Now I just have to figure out how to get it to work with words. Thanks for all the help guys.
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  8. #38
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Standard C++ headers do not have .h on the end. If I wanted to use the C standard header stdlib.h, typically C++ headers drop the .h and add a 'c' to the front, thus <cstdlib>.

  9. #39
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Indeed, none of the C++ standard headers include a .h; for example, #include <iostream> is standard, #include <iostream.h> is NOT standard, and not guaranteed to work.

  10. #40
    Registered User
    Join Date
    May 2003
    Posts
    43
    Oh... There have been times where dropping the .h hasn't worked for me, so I tend to leave the .h there. Also, I've gotten the program to work for sorting numbers that the user types in, but sometimes when I use some 3 or 4 digit numbers it'll mess up. For example, I can put in 9, 7054, 7654, 745, and 5. The output will end up being:

    9
    5
    745
    7045
    7654

    Does anybody know why that might be? The code is the exact same as Cat's in his last post, with the exception of the addition of 5 new variables, a prompt to enter some numbers, and the numbers being replaced by the variables.
    Programmer's Law:

    If your program doesn't work, look for the part that you didn't think was
    important.

  11. #41
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main() {
    
            vector<int> numbers;
            int temp;
    
            cout <<"Enter five numbers to sort: " <<endl;
    
            for( int iter = 1; iter <= 5; iter++ ) {
                    cout <<"#" <<iter <<": ";
    
                    cin >>temp;
                    numbers.push_back( temp );
            }
    
            sort( numbers.begin(), numbers.end() );
    
            cout <<"\nSorted list: " <<endl;
    
            for( int index = 0; index < numbers.size(); index++ ) 
                    cout <<numbers[index] <<endl;
    
    }
    That's the C++ solution to your problem. If you don't know what vectors are or how to use them, I strongly suggest learning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem sorting an array of strings
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 05-07-2008, 09:02 PM
  2. strings, sorting
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 02-18-2006, 04:29 PM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. bubble sorting multiple strings
    By jibbles in forum C Programming
    Replies: 10
    Last Post: 10-11-2003, 11:48 PM
  5. Sorting strings to speed up search?
    By Mox in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2001, 12:17 PM