Hello all and thanks for the help ahead of time I'm in need of a little help. I need to edit the program below to only bring back the highest GPA with all the other information attached. I think its pretty simple to get the highest GPA by just having the fillVector() hold this little GPA > x then x = GPA (if i am wrong feel free to correct me), but idk how to attach all other information with that any hints would be very much appreciated( I suspect there is a way to save the point in the vector rather than just the information saved in gpa, but have no clue as how to do it).
One of the simplest ways to find the highest gpa is to have a separate function that will do the following:
1) Initialize a gpa_comparison field to 0.
2) In a loop, do the following:
a) Check if the current student's gpa is higher than the gpa_comparison field.
b) If not, consider the next student.
c) If it is, save the gpa into the gpa_comparison field.
3) At the end of the previous loop, you now have saved the highest gpa. In another loop, iterate through the vector, and for each student with this gpa, print all the attributes associated with that student.
Of course, this algorithm is inefficient, since it requires multiple iterations to scroll through the data.
An alternative is to use a multiset<> container and populate it, instead of a vector. Then you will get an automatic sort gpa-wise, since a multiset<> is an ordered container.
void printHighestGpa()
{
double highestGpa = 0;
for (unsignedint i = 0; i < size; i++)
{
if (newAllStudenst[i].getGpa() > highestGpa)
{
newAllStudent[i].getGpa() = highestGpa; /* then i get stumped I know i will return the highestGpa but what about name and year and rank? Do I just set up the other info in the if loop as well?*/
}
}
}
also ive been researching this question off site and ran into *min_element and *max_element would this be an option for this program? Only reason I ask is because i see everyone using arrays with this but no vectors.
I see you output the results EVERY time that you find a new highest value. Perhaps you should wait until you've gone right through the vector and THEN output just once.
Thanks Repeater thats exactly the hint i needed I moved the cout statements outside the for loop and it works great again thanks so very much to all that helped me!
Actually, there is a serious bug in your logic and it won't work correctly at all.
You are trying to obtain the highest Gpa and print it (along with the other values) in a single iteration. If your I/P data 's 1st student contains a gpa of 4.5 and 2nd student a gpa of 4.6, both will be printed.
You must have 2 iterations:
The 1st iteration will return the highest gpa.
In the 2nd iteration, compare this returned value with the gpa of each student. If it matches, print it out.
Ill run the program i have created again and see if it does this with 3.x and 2.xas a student shouldnt have a gpa over 4.0. If I was writing this program for a purpose I would include error checking to be sure the user only inputs a number between 0.1 and 4.0 but this is just a school project and the teacher isnt expecting it as the time constraints we are under.
I ran my newest code here with the parameters you set up but I cannot replicate and get the student who won the scholarship = the one with a gpa of 4.6 only not the 4.5