| | |
Sort in descending order....
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
If I want to sort in descending order for exams.at (i).total.at(j)
like this..
the output
1 34
2 56
3 21
and I want to cout the output
2 56
1 34
3 21
Is it must to use bubble sort or radix sort?
like this..
the output
1 34
2 56
3 21
and I want to cout the output
2 56
1 34
3 21
Is it must to use bubble sort or radix sort?
c++ Syntax (Toggle Plain Text)
#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 { string examid; vector <int> total; }; int main() { ifstream stream1("STA83SOLUTION.txt"); if ( !stream1.is_open()) { cout << "While opening a file an error is encountered" << endl; } else { cout << "Fail Di buka....." << endl; } vector <exam> exams; exam aExam; string 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("408.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++) { myfile<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student } } } cin.get(); return 0; }
You can use std::sort() algorithm and write your own comparison function for it. Here is an example And you can find more information in these google links
Last edited by Ancient Dragon; Apr 7th, 2008 at 11:55 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
I do like this but abnormal program........
Fail Di buka.....
abnormal program termination
Press any key to continue
C++ Syntax (Toggle Plain Text)
#include <iostream> // std::cout #include <fstream> #include <iomanip> #include <string> // std::string #include <vector> // std::vector<> #include <algorithm> //std::for each() #include <functional> using namespace std;// import "std" namespace into global namespace struct exam { string examid; vector <int> total; }; int main() { ifstream stream1("STA83SOLUTION.txt"); if ( !stream1.is_open()) { cout << "While opening a file an error is encountered" << endl; } else { cout << "Fail Di buka....." << endl; } vector <exam> exams; exam aExam; string 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("408.txt"); int flag = 1; // set flag to 1 to begin initial pass int temp; // holding variable if (myfile.is_open()) { for (size_t i = 0; i < exams.size(); i++) flag = 0; for (size_t j = 0; j<(exams.at(i).total.size()-1); j++) { myfile<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student } if (exams.at(i).total.size()+1 > exams.at(i).total.size() ) // ascending order simply changes to < { temp = exams.at (i).total.at (j); // swap elements exams.at (i).total.at (j) = exams.at (i).total.at (j+1); exams.at (i).total.at (j+1) = temp; flag = 1; // indicates that a swap occurred. } } } cin.get(); return 0; }
Fail Di buka.....
abnormal program termination
Press any key to continue
I done something sort like this..but error..anybody can show me where is the mistake?
: missing ',' before '.'
syntax error : '.'
error C2601: 'sortit' : local function definitions are illegal
Error executing cl.exe.
: missing ',' before '.'
syntax error : '.'
error C2601: 'sortit' : local function definitions are illegal
Error executing cl.exe.
C++ Syntax (Toggle Plain Text)
#include <iostream> // std::cout #include <fstream> #include <iomanip> #include <string> // std::string #include <cstdlib> #include <cstddef> #include <vector> // std::vector<> #include <algorithm> //std::for each() using namespace std;// import "std" namespace into global namespace struct exam { string examid; vector <int> total; }; int main() { ifstream stream1("STA83SOLUTION.txt"); if ( !stream1.is_open()) { cout << "While opening a file an error is encountered" << endl; } else { cout << "Fail Di buka....." << endl; } vector <exam> exams; exam aExam; string 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("408.txt"); void sortit(int exams.at(i).total.at(j), int exams.size()) { int temp; // holding variable 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++) { myfile<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j) <<"\t"; // output list of exam codes for this student if ( exams.at (i).total.at(j) < exams.at (i).total.at(j+1) ) { temp = exams.at (j).total.at(j+1); exams.at (i).total.at(j+1) =exams.at (i).total.at(j) ; exams.at (i).total.at(j) = temp; } } } reversesort(num, sizeof num / sizeof *num); cout<<"The list in descending order is:\n"; for ( i = 0; i < exams.size() ; ++i ) { cout << num[i] << "\n"; } }} cin.get(); return 0; }
You can't nest functions like this:
C++ Syntax (Toggle Plain Text)
int main() { [...] void sortit(int exams.at(i).total.at(j), int exams.size()) { [...] } }
un-nest the two functions:
C++ Syntax (Toggle Plain Text)
int main() { //do stuff some_var = sortit(.... , ... ); } void sortit(int exams.at(i).total.at(j), int exams.size()) { //do stuff return something; }
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
I think you need to understand what a function is and how to use one, here is a short basic tutorial
http://www.cplusplus.com/doc/tutorial/functions.html
and
http://www.cplusplus.com/doc/tutorial/functions2.html
http://www.cplusplus.com/doc/tutorial/functions.html
and
http://www.cplusplus.com/doc/tutorial/functions2.html
![]() |
Similar Threads
- How to sort?? (C++)
- how to read a csv file and sort the specific clolumn in descending order (C#)
- Open In New Window Php (PHP)
- Selection Sort on a string with char and int? (C++)
- help with descending sort program (C++)
- how do i sort reords from Z-A? (in turbo C) (C++)
- Some help needed with Selection Sort in java, Please!! (Java)
- Vectors, Functions, and Sorting... oh my! (C++)
- beginner"problem in arrays help me in codes" (C)
Other Threads in the C++ Forum
- Previous Thread: is knowlegde of C++ enough for this program?
- Next Thread: Why
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






