#C13335. Calculate Average Grades and Sort Students
Calculate Average Grades and Sort Students
Calculate Average Grades and Sort Students
You are given a JSON array representing a list of students. Each student is an object with a name
(a string) and grades
(an array of integers).
Your task is to:
- Compute the average grade for each student using the formula: $$\text{average} = \frac{\sum_{i=1}^{n} grade_i}{n}$$. Round the result to two decimal places.
- Add the computed average to the corresponding student object with the key
average
. - Sort the list of students in descending order by average grade.
- Output the sorted list in JSON format.
Note: The input is provided as a JSON formatted string via standard input and the output should be printed to standard output.
inputFormat
The input consists of a single JSON array provided via standard input. Each element in the array is a JSON object with the following structure:
{ "name": "Student Name", "grades": [grade1, grade2, ..., gradeN] }
There are no extra lines in the input.
outputFormat
The output should be a JSON array printed to standard output. Each object in the array must contain the original name
and grades
, and an additional average
key representing the student's average grade (rounded to two decimal places). The array must be sorted in descending order by the average
value.
[
{"name": "John Doe", "grades": [88, 92, 79]},
{"name": "Jane Smith", "grades": [85, 90, 95]},
{"name": "Emily Davis", "grades": [70, 75, 80]}
]
[
{
"name": "Jane Smith",
"grades": [85, 90, 95],
"average": 90.0
},
{
"name": "John Doe",
"grades": [88, 92, 79],
"average": 86.33
},
{
"name": "Emily Davis",
"grades": [70, 75, 80],
"average": 75.0
}
]
</p>