#C13269. Calculate Average Grades
Calculate Average Grades
Calculate Average Grades
You are given a JSON array that represents a list of students. Each student is represented by a JSON object which always has a name
key and may contain several subject scores. Your task is to compute the average grade for each student using the formula:
\( \text{average} = \frac{\text{sum of subject scores}}{\text{number of subjects}} \)
If a student does not have any subject scores, the average should be 0. The result should be a JSON array of objects, each with fields name
and average
. The average must be rounded to one decimal place if applicable.
inputFormat
The input is read from stdin as a single JSON array. Each element in the array is a JSON object representing a student. A student object always contains a name
key (a string) and zero or more subject keys with numeric values.
Example input:
[ {"name": "Alice", "math": 90, "science": 80, "english": 85}, {"name": "Bob", "math": 75, "science": 70, "english": 65} ]
outputFormat
The output should be written to stdout as a JSON array of objects. Each object must have two keys: name
(the student's name) and average
(the computed average score as per the formula, rounded to one decimal place if needed).
Example output:
[{"name":"Alice","average":85.0},{"name":"Bob","average":70.0}]## sample
[
{"name": "Alice", "math": 90, "science": 80, "english": 85},
{"name": "Bob", "math": 75, "science": 70, "english": 65},
{"name": "Charlie", "math": 100, "science": 95, "english": 90}
]
[{"name":"Alice","average":85.0},{"name":"Bob","average":70.0},{"name":"Charlie","average":95.0}]