sort

This is a discussion on sort within the C++ Programming forums, part of the General Programming Boards category; i have this data..but how to sort the data in descending order together with the examid? examid total 1: 25 ...

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

    Post sort

    i have this data..but how to sort the data in descending order together with the examid?

    examid total
    1: 25
    2: 20
    3: 46
    4: 56
    5: 12
    6: 22
    7: 20
    8: 18

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    It depends on how you structure the data, but you can use std::sort() with a suitable comparator (e.g., a function object that returns true if the first total is greater than the second total).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    21
    IS IT TRUE I STRUCT TEH DATA LIKE THIS?

    Code:
    #include <iostream>   // std::cout
    #include <fstream>
    #include <iomanip>
    #include <string>    // std::string
    #include <vector>    // std::vector<>
    #include <algorithm> //std::for each()
    using namespace std; // import "std" namespace into global namespace
    
    struct exam
    {
    int examid;
    vector <int> total;
    };
    	int main() 
    	{
    	ifstream stream1("STA83SOLUTION.txt");
    		if ( !stream1.is_open())
    		{
    		cout << "Tak Buka" << endl;
    		} 
    			else 
    			{
    			cout << "Fail Di buka....." << endl;
    			}
    	vector <exam> exams;
    	exam aExam;
        int tempExamID;
        int tempTotal;
        stream1 >> tempExamID >> tempTotal;
        aExam.examid = tempExamID;
        aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
        while (stream1 >> tempExamID >> tempTotal)
        {
            if(tempExamID != aExam.examid)
            {
            exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
            aExam.total.clear();
            aExam.examid = tempExamID;
            }
            aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
        }
        exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
        stream1.close(); // We have read the entire file, so time to close it.
    {
    	ofstream myfile;
    	myfile.open("411.txt");
    	
    
    	if (myfile.is_open())
    	{
    		for (size_t i = 0; i < exams.size(); i++) 
    		{
    			for (size_t j = 0; j<exams.at(i).total.size(); j++) 
    			{
    		cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
    	  		}
    		}
    	}				
    cin.get();
    return 0;
    }
    }

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    It could be a lot easier if you used a class. Split the code into various functions, for example one could sort the passed data like laserlight suggested, another could display it after it has sorted it.
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    21
    How to split the code into various functions?

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    well, you would have a class like this:

    Code:
    class Foo
    {
    public:
       Foo(); // initalise date members here
       ~Foo();
    
    void sort ( pass your vector here );
    std::vector displaySorted ( this would display your sorted values ) const;
    
    private:
     // declare the vector and other variables here
    };
    By using this sort of structure, you can organise the code a lot better. A strut is ok when you have lots of date refereing to the same type, but a struct in theory doesnt use functions. Where a class can do.
    I'm just trying to be a better person - My Name Is Earl

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    21
    Foo means what..a cinese language?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    foo is a short name used for something in examples by programmers when they have no other better name. It has a meaning, look up foo (or foobar) in Wikipedia.

    --
    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.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by nurulshidanoni View Post
    How to split the code into various functions?
    Have you even opened your C++ book yet?

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    You've mixed tabs and spaces in your source file which will make it look horrible here. Please fix that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    21

    Foo

    Yes..I have see your homerpage..but i have two arrays..I dont know to use a class Foo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 04:21 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM
  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