#C13091. Calculate Average Scores for Students
Calculate Average Scores for Students
Calculate Average Scores for Students
You are given a JSON array via standard input, where each element is an object representing a student. Each student object contains a string field name
and a nested object scores
with keys Mathematics
, Science
, and English
representing the student’s scores. Your task is to compute the average score for each student using the formula ( average = \frac{Mathematics + Science + English}{3} ) (rounded to two decimal places) and output a JSON array of objects containing the student's name and the computed average score. If any of the three required subjects is missing or has a value that cannot be converted to a number, skip that student. In case the input JSON is invalid, print an appropriate error message.
inputFormat
The input is provided via standard input as a single JSON array. Each element in the array is a JSON object that has a name
field and a scores
field. The scores
field is an object containing the keys Mathematics
, Science
, and English
with numeric values (which may be represented as integers or floats).
outputFormat
Output to standard output a JSON array of objects. Each object should contain the name
of a student and their corresponding average_score
(rounded to two decimal places). Students with incomplete or invalid score data should be omitted from the output.## sample
[{"name": "Alice", "scores": {"Mathematics": 85, "Science": 78, "English": 92}}, {"name": "Bob", "scores": {"Mathematics": 90, "Science": 88, "English": 84}}]
[{"name":"Alice","average_score":85.0},{"name":"Bob","average_score":87.33}]