Lab 2 Exercise 2
You cannot submit for this problem because the homework's deadline is due.
Related Topics: function, struct, qsort, pointer
Description
In a high school, all final exam grades have been released. The teachers want to rank the students according to their total score. If two students have the same total score, just keep the order the same as in the input. As a programming expert, can you help design a system that solves this problem elegantly?
Requirement
You are required to use a struct
to store information for each student. Also, you are required to use qsort()
in <cstdlib>
to achieve sorting. Check http://www.cplusplus.com/reference/cstdlib/qsort/ on how to use that.
Input Format
The first line: an integer n (1<= n <= 10)
that stands for the number of students. The following \(n\) lines: Name(string) ChineseScore(int) MathScore(int) EnglishScore(int). You can assume that there is no space in name and the length is less than 16
.
Output Format
Students sorted in descending total score.
Sample Input
3
sam 110 132 140
tom 105 140 140
david 90 150 120
Sample Output
# sample
tom 105 140 140
sam 110 132 140
david 90 150 120
Takeaway
In this exercise, we sorted an array of structs. Whenever you want to compact information
, struct could be a nice choice. Also, we learned how to write a compare function
for qsort()
. Referring to documentation
is the most efficient way to learn how to use a new function.