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
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
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).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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; } }
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.
Double Helix STL
How to split the code into various functions?
well, you would have a class like this:
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.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 };
Double Helix STL
Foo means what..a cinese language?
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.
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"
Yes..I have see your homerpage..but i have two arrays..I dont know to use a class Foo.