Sort in descending order....

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Sort in descending order....

 
0
  #1
Apr 7th, 2008
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?

  1. #include <iostream> // std::cout
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string> // std::string
  5. #include <vector> // std::vector<>
  6. #include <algorithm> //std::for each()
  7. using namespace std;// import "std" namespace into global namespace
  8.  
  9. struct exam
  10. {
  11. string examid;
  12. vector <int> total;
  13. };
  14.  
  15. int main()
  16. {
  17. ifstream stream1("STA83SOLUTION.txt");
  18. if ( !stream1.is_open())
  19. {
  20. cout << "While opening a file an error is encountered" << endl;
  21. }
  22. else
  23. {
  24. cout << "Fail Di buka....." << endl;
  25. }
  26. vector <exam> exams;
  27. exam aExam;
  28. string tempExamID;
  29. int tempTotal;
  30. stream1 >> tempExamID >> tempTotal;
  31. aExam.examid = tempExamID;
  32. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  33. while (stream1 >> tempExamID >> tempTotal)
  34. {
  35. if(tempExamID != aExam.examid)
  36. {
  37. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  38. aExam.total.clear();
  39. aExam.examid = tempExamID;
  40. }
  41. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  42. }
  43. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  44. stream1.close(); // We have read the entire file, so time to close it.
  45. {
  46. ofstream myfile;
  47. myfile.open("408.txt");
  48. if (myfile.is_open())
  49. {
  50. for (size_t i = 0; i < exams.size(); i++)
  51. for (size_t j = 0; j<exams.at(i).total.size(); j++)
  52. {
  53. myfile<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
  54. }
  55. }
  56. }
  57. cin.get();
  58. return 0;
  59. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Sort in descending order....

 
0
  #2
Apr 7th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: Sort in descending order....

 
0
  #3
Apr 8th, 2008
How can sort because I have two for ..which is i and j?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: Sort in descending order....

 
0
  #4
Apr 8th, 2008
I do like this but abnormal program........

  1. #include <iostream> // std::cout
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string> // std::string
  5. #include <vector> // std::vector<>
  6. #include <algorithm> //std::for each()
  7. #include <functional>
  8. using namespace std;// import "std" namespace into global namespace
  9.  
  10. struct exam
  11. {
  12. string examid;
  13. vector <int> total;
  14. };
  15.  
  16. int main()
  17. {
  18. ifstream stream1("STA83SOLUTION.txt");
  19. if ( !stream1.is_open())
  20. {
  21. cout << "While opening a file an error is encountered" << endl;
  22. }
  23. else
  24. {
  25. cout << "Fail Di buka....." << endl;
  26. }
  27. vector <exam> exams;
  28. exam aExam;
  29. string tempExamID;
  30. int tempTotal;
  31. stream1 >> tempExamID >> tempTotal;
  32. aExam.examid = tempExamID;
  33. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  34. while (stream1 >> tempExamID >> tempTotal)
  35. {
  36. if(tempExamID != aExam.examid)
  37. {
  38. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  39. aExam.total.clear();
  40. aExam.examid = tempExamID;
  41. }
  42. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  43. }
  44.  
  45. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  46. stream1.close(); // We have read the entire file, so time to close it.
  47. {
  48. ofstream myfile;
  49. myfile.open("408.txt");
  50. int flag = 1; // set flag to 1 to begin initial pass
  51. int temp; // holding variable
  52. if (myfile.is_open())
  53. {
  54. for (size_t i = 0; i < exams.size(); i++)
  55. flag = 0;
  56. for (size_t j = 0; j<(exams.at(i).total.size()-1); j++)
  57. {
  58. myfile<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
  59. }
  60.  
  61. if (exams.at(i).total.size()+1 > exams.at(i).total.size() ) // ascending order simply changes to <
  62. {
  63. temp = exams.at (i).total.at (j); // swap elements
  64. exams.at (i).total.at (j) = exams.at (i).total.at (j+1);
  65. exams.at (i).total.at (j+1) = temp;
  66. flag = 1; // indicates that a swap occurred.
  67. }
  68. }
  69. }
  70. cin.get();
  71. return 0;
  72. }











Fail Di buka.....

abnormal program termination
Press any key to continue
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: Sort in descending order....

 
0
  #5
Apr 8th, 2008
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.

  1. #include <iostream> // std::cout
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string> // std::string
  5. #include <cstdlib>
  6. #include <cstddef>
  7. #include <vector> // std::vector<>
  8. #include <algorithm> //std::for each()
  9.  
  10. using namespace std;// import "std" namespace into global namespace
  11.  
  12. struct exam
  13. {
  14. string examid;
  15. vector <int> total;
  16. };
  17.  
  18. int main()
  19. {
  20. ifstream stream1("STA83SOLUTION.txt");
  21. if ( !stream1.is_open())
  22. {
  23. cout << "While opening a file an error is encountered" << endl;
  24. }
  25. else
  26. {
  27. cout << "Fail Di buka....." << endl;
  28. }
  29. vector <exam> exams;
  30. exam aExam;
  31. string tempExamID;
  32. int tempTotal;
  33. stream1 >> tempExamID >> tempTotal;
  34. aExam.examid = tempExamID;
  35. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  36. while (stream1 >> tempExamID >> tempTotal)
  37. {
  38. if(tempExamID != aExam.examid)
  39. {
  40. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  41. aExam.total.clear();
  42. aExam.examid = tempExamID;
  43. }
  44. aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
  45. }
  46. exams.push_back(aExam); // no more exam codes for this student. Add aStudent to students vector
  47. stream1.close(); // We have read the entire file, so time to close it.
  48. {
  49. ofstream myfile;
  50. myfile.open("408.txt");
  51.  
  52. void sortit(int exams.at(i).total.at(j), int exams.size())
  53. {
  54.  
  55. int temp; // holding variable
  56. if (myfile.is_open())
  57. {
  58. for (size_t i = 0; i < exams.size(); i++)
  59. for (size_t j = 0; j<(exams.at(i).total.size()); j++)
  60. {
  61. myfile<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j) <<"\t"; // output list of exam codes for this student
  62. if ( exams.at (i).total.at(j) < exams.at (i).total.at(j+1) )
  63. {
  64. temp = exams.at (j).total.at(j+1);
  65. exams.at (i).total.at(j+1) =exams.at (i).total.at(j) ;
  66. exams.at (i).total.at(j) = temp;
  67. }
  68. }
  69.  
  70. } reversesort(num, sizeof num / sizeof *num);
  71. cout<<"The list in descending order is:\n";
  72. for ( i = 0; i < exams.size() ; ++i )
  73. {
  74. cout << num[i] << "\n";
  75. }
  76.  
  77. }}
  78. cin.get();
  79. return 0;
  80. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,880
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Sort in descending order....

 
0
  #6
Apr 8th, 2008
You can't nest functions like this:
  1. int main()
  2. {
  3. [...]
  4. void sortit(int exams.at(i).total.at(j), int exams.size())
  5. {
  6. [...]
  7. }
  8. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 219
Reputation: nurulshidanoni is an unknown quantity at this point 
Solved Threads: 0
nurulshidanoni's Avatar
nurulshidanoni nurulshidanoni is offline Offline
Posting Whiz in Training

Re: Sort in descending order....

 
0
  #7
Apr 8th, 2008
So, can I combine it? void main?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,880
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Sort in descending order....

 
0
  #8
Apr 8th, 2008
un-nest the two functions:

  1. int main()
  2. {
  3. //do stuff
  4. some_var = sortit(.... , ... );
  5. }
  6.  
  7. void sortit(int exams.at(i).total.at(j), int exams.size())
  8. {
  9. //do stuff
  10. return something;
  11. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Sort in descending order....

 
0
  #9
Apr 8th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC