help for task

closed account (NTkE8vqX)
Thank you soo much
Last edited on
What have you written so far?
closed account (NTkE8vqX)


nothing good and you can see that...I don't now how to make program to cout the students name last name and grades sorted from highest grade to lowest...
Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has syntax highlighting and line numbers, as well as proper indentation.

Are you allowed to use std::vector and std::sort? If so, it will be pretty simple. If not, you're in for some pain.
closed account (NTkE8vqX)
Yes I am allowed,but is not simple for me because this first time that i met with programing and I don't now functions very good in the any program language ...
First, you will want to create a structure to represent a student:
1
2
3
4
5
6
7
8
9
struct Student
{
    std::string fname; //first name
    std::string lname; //last name
    //Use a map for subject -> grade mapping
    // http://www.cplusplus.com/reference/map/map/
    // http://en.cppreference.com/w/cpp/container/map
    std::map<std::string, int> grades;
};
Then, you'll want to store the students in a std::vector inside your main function:
1
2
3
4
5
int main()
{
    std::vector<Student> students;
    //...
}
To add students, you would create a temporary student object, prompt the user to input values into it, and then copy it into the vector with .push_back().

When you want to sort by grades, you can create a comparison function to use in std::sort:
1
2
3
4
5
bool highest_grade_average(Student const &a, Student const &b)
{
    //take the average of each student's grades
    //return true if a has a higher average than b, false otherwise
}
1
2
std::sort(std::begin(students), std::end(students), highest_grade_average);
//... that's it! 
Last edited on
closed account (NTkE8vqX)
Thank you soo much
Last edited on
This is not a homework solutions site. We don't do immoral things like that here. Many colleges and universities have strict policies: having others complete your assignments will likely get you expelled.

We are perfectly willing to help you to learn how to do it yourself. We will not do it for you - it is wrong.
Topic archived. No new replies allowed.