How to sort the data???

This is a discussion on How to sort the data??? within the C++ Programming forums, part of the General Programming Boards category; Here is my code... Code: void Show_Ranking(float Time_Diff[], int Attempts[], int No_Participants, char Name_Participants[][128]) { int smallest_value; int start_value; int ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    14

    How to sort the data???

    Here is my code...
    Code:
    void Show_Ranking(float Time_Diff[], int Attempts[], int No_Participants, char Name_Participants[][128])
    { 
    	int smallest_value;
    	int start_value;
    	int current_value;
    	int ctrl;
    
    	for(start_value=0; start_value<No_Participants; start_value++)
    	{
    		smallest_value=start_value;
    
    		for(current_value=start_value+1; current_value<No_Participants; current_value++)
    		{
    			if(Attempts[current_value] < Attempts[smallest_value])
    			{
    				smallest_value=start_value;
    			}
    
    			else
    				if(Attempts[current_value] == Attempts[smallest_value] &&
    					Time_Diff[current_value]<Time_Diff[smallest_value])
    				{
    					smallest_value=start_value;
    				}
    		}
    
    		swap(Attempts[start_value],Attempts[smallest_value]);
    		swap(Name_Participants[start_value],Name_Participants[smallest_value]);
    	}
    
    		for(ctrl=0; ctrl<No_Participants; ctrl++)
    		{
    			cout << ctrl+1 << "\t" << Name_Participants[ctrl];
    			cout << endl;
    		}
    }
    This is a multi player mastermind game, now i'm stuck with how to sort the participant with their attempts?? And if their attempts is same, it will sort with respect to the time??

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    1. Why are you using arrays? This is C++, you should look into the STL. Specifically I would look at std::vector and then bust out std::sort. Here is a quick example, however you would drop the array all together.
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    
    int main() {
        //this would be user input
        int userints[] = {5, 3, 40, 25, 6, 1, 34};
    
        std::vector<int> myVector(userints,
                                  userints + sizeof(userints) / sizeof(userints[0]));
    
        std::cout << "Before sort:\n";
        std::copy(myVector.begin(), myVector.end(),
                  std::ostream_iterator<int>(std::cout, " "));
        std::sort(myVector.begin(),myVector.end());
        std::cout<<"\nAfter sort:\n";
        std::copy(myVector.begin(), myVector.end(),
                  std::ostream_iterator<int>(std::cout, " "));
        std::cout << std::endl;
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    Perhaps consider a map, and the maybe the line
    Code:
    using namespace std;
    With the map you can have an associative collection,
    Code:
    map<string, int> player;
    so that would be a list of names associated with a score, you won't get the ::sort function but it's no disaster here, there are regular sorts you could write just relying on string comparisons.

    Not sure if the map is the very best approach, but it strikes me as of interest in this scenario.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    sort is a good start, but you'll still need to write your own comparator to sort on multiple criteria. Step 1 would seem to be to recognize that <name, attempts, time> is one "thing" and create a datatype to hold that thing.

    To actually write your "BetterThan" function, you would first see if the attempts are different -- if they are you can return true/false based on the attempts. If the attempts are the same, you can then check the time afterwards. (EDIT: And actually this is what you've got written, which means you just need to actually do all the entries and not forget about swapping time, which is why you make your datatype so that everything changes together.)
    Last edited by tabstop; 08-24-2011 at 09:19 AM.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    14
    Quote Originally Posted by AndrewHunter View Post
    1. Why are you using arrays? This is C++, you should look into the STL. Specifically I would look at std::vector and then bust out std::sort. Here is a quick example, however you would drop the array all together.
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    
    int main() {
        //this would be user input
        int userints[] = {5, 3, 40, 25, 6, 1, 34};
    
        std::vector<int> myVector(userints,
                                  userints + sizeof(userints) / sizeof(userints[0]));
    
        std::cout << "Before sort:\n";
        std::copy(myVector.begin(), myVector.end(),
                  std::ostream_iterator<int>(std::cout, " "));
        std::sort(myVector.begin(),myVector.end());
        std::cout<<"\nAfter sort:\n";
        std::copy(myVector.begin(), myVector.end(),
                  std::ostream_iterator<int>(std::cout, " "));
        std::cout << std::endl;
    }
    I'm using array because this is what I'm told to do...
    And I'm still new to C++

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by justin8077 View Post
    I'm using array because this is what I'm told to do...
    And I'm still new to C++
    This is a school project? It appeared that you were making a personal one; hmm at least this is an interesting project. Well, if you must stick with arrays then tabstop's advice is still valid. Take a look at combining <name,attempts,time> into one object and then make an array of those objects. From there call your sorting function.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sort a 2d Array and keep data intact
    By rogster001 in forum Contests Board
    Replies: 13
    Last Post: 01-23-2011, 12:09 AM
  2. Replies: 2
    Last Post: 03-05-2005, 03:00 PM
  3. How To SORT DATA In Database With C/C++?
    By javacvb in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2003, 09:53 AM
  4. sort data
    By sdchem in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2003, 08:40 AM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 07:05 PM

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