Monday, May 24, 2010

C++ Program Help?

If you guys have been following my last questions you will see that I have learned alot but still need some help. I have finally gotten my program to work with the vector of structs. Now I need to be able to show the name(s) of the highest scorer(s) and the name(s) of the lowest scorer(s).





Here is my program thus far it is error free as of now.





// In a header file


#ifndef GRADES_H


#define GRADES_H





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using namespace std;





struct Grades


{


string names;


int scores;


};





#endif

C++ Program Help?
You are progressing well. In order to print out the names with the highest and lowest scores, you will need to first sort the vector VGrade. That is one way of doing it - a way that uses the STL sort() function from algorithm.h file. But there are other ways. Since you want to know how to do it using the algorithm.h function, I will show you how to do that.





First of all, what you must understand is that the sort() function takes a vector's iterators and sorts all the elements within those iterators. So if you had a vector of integers v:





std::vector%26lt;int%26gt; v;





then you could call, after populating v with some values:





sort(v.begin(), v.end());





It would work. v will get sorted, and if you print out the 1st value of v, it will be the least value, and the last one will be the greatest value - thereby serving your purpose.





But in your case, it is slightly different because your vector is not of ints! Your vector is of GRADE-s, which is a "user defined datatype". So if you try to sort your vector, C++ will immediately ask you back - well, how do I know whether GRADE1 (an instance of GRADE) is %26gt; or %26lt; or = GRADE2?





Reason is simple: in order to sort, it needs to compare the values and determine which is higher and which is lower relative to the other values!





With ints, that was easy. C++ can tell whether 49 is greater than 72. But how will C++ tell whether GRADE1{Name:Koushik, Score:75} is greater than GRADE2{Name:John, Score:21}?





Therefore, first you need to provide the comparison operator less than (%26lt;) for your struct. Only one comparison operator is needed (you do not need to define "more than").





So go back to your code, and add this global function at the top (or in the header file):





bool operator%26lt;(const struct Grades%26amp; a, const struct Grades%26amp; b)


{


return a.scores %26lt; b.scores;


}





What this function does is it gives the compiler a way to tell which instance of a GRADES struct is higher and which is lower. And you just told it to use the "scores" field to decide that.





After doing that, just use the sort() method:





std::sort( VGrade.begin(), VGrade.end() );





After you do that, VGrade[0].scores is the lowest and VGrade[4].scores is the highest, and VGrade[0].names is the guy with the least score and VGrade[4].names is the guy with the highest score!

apricot

No comments:

Post a Comment