# Students with maximum average score of three subjects

• Last Updated : 09 Jun, 2021

Given a file containing data of student name and marks scored by him/her in 3 subjects. The task is to find the list of students having the maximum average score.
Note : If more than one student has the maximum average score, print them as per the order in the file.
Examples:

Input : file[] = {“Shrikanth”, “20”, “30”, “10”, “Ram”, “100”, “50”, “10”}
Output : Ram 53
Average scores of Shrikanth, Ram are 20 and 53 respectively. So Ram has the maximum average score of 53.
Input : file[] = {“Ramesh”, “90”, “70”, “40”, “Adam”, “50”, “10”,
”40″, “Suresh”, “22”, “1”, “56”, “Rocky”, “100”, “90”, “10”}
Output : Ramesh Rocky 66
Average scores of Ramesh, Adam, Suresh and Rocky are 66, 33, 26 and 66 respectively. So both Ramesh and Rocky have the maximum average score of 66.

Approach :

1. Traverse the file data and store average scores for each student.
2. Now, find the maximum average score and search for all the students with this maximum average score.
3. Print the maximum average score and names as per the order in the file.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the` `// list of students having maximum average score` `#include` `using` `namespace` `std;`   `// Function to find the` `// list of students having maximum average score` `// Driver code` `void` `getStudentsList(string file[],``int` `n)` `{` `    ``// Variables to store average score of a student` `    ``// and maximum average score` `    ``int` `avgScore;` `    ``int` `maxAvgScore = INT_MIN;`   `    ``// List to store names of students` `    ``// having maximum average score` `    ``vector names;`   `    ``// Traversing the file data` `    ``for` `(``int` `i = 0; i < n; i += 4) {`   `        ``// finding average score of a student` `        ``avgScore = (stoi(file[i + 1]) + ` `                    ``stoi(file[i + 2]) + ` `                ``stoi(file[i + 3])) / 3;`   `        ``if` `(avgScore > maxAvgScore) {` `            ``maxAvgScore = avgScore;`   `            ``// Clear the list and add name of student` `            ``// having current maximum average score in the list` `            ``names.clear();` `            ``names.push_back(file[i]);` `        ``}`   `        ``else` `if` `(avgScore == maxAvgScore)` `            ``names.push_back(file[i]);` `    ``}`   `    ``// Printing the maximum average score and names` `    ``// of students having this maximum average score` `    ``// as per the order in the file.` `    ``for` `(``int` `i = 0; i < names.size(); i++) {` `        ``cout <

## Java

 `// Java program to find the` `// list of students having maximum average score`   `import` `java.io.*;` `import` `java.util.*;` `import` `java.lang.*;`   `class` `GFG {`   `    ``// Function to find the` `    ``// list of students having maximum average score` `    ``// Driver code` `    ``static` `void` `getStudentsList(String[] file)` `    ``{` `        ``// Variables to store average score of a student` `        ``// and maximum average score` `        ``int` `avgScore;` `        ``int` `maxAvgScore = Integer.MIN_VALUE;`   `        ``// List to store names of students` `        ``// having maximum average score` `        ``ArrayList names = ``new` `ArrayList<>();`   `        ``// Traversing the file data` `        ``for` `(``int` `i = ``0``; i < file.length; i += ``4``) {`   `            ``// finding average score of a student` `            ``avgScore = (Integer.parseInt(file[i + ``1``]) + ` `                        ``Integer.parseInt(file[i + ``2``]) + ` `                       ``Integer.parseInt(file[i + ``3``])) / ``3``;`   `            ``if` `(avgScore > maxAvgScore) {` `                ``maxAvgScore = avgScore;`   `                ``// Clear the list and add name of student` `                ``// having current maximum average score in the list` `                ``names.clear();` `                ``names.add(file[i]);` `            ``}`   `            ``else` `if` `(avgScore == maxAvgScore)` `                ``names.add(file[i]);` `        ``}`   `        ``// Printing the maximum average score and names` `        ``// of students having this maximum average score` `        ``// as per the order in the file.` `        ``for` `(``int` `i = ``0``; i < names.size(); i++) {` `            ``System.out.print(names.get(i) + ``" "``);` `        ``}`   `        ``System.out.print(maxAvgScore);` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``String[] file = { ``"Shrikanth"``, ``"20"``, ``"30"``, ` `                        ``"10"``, ``"Ram"``, ``"100"``, ``"50"``, ``"10"` `};` `        ``getStudentsList(file);` `    ``}` `}`

## Python3

 `# Python3 program to find the list of ` `# students having maximum average score `   `# Function to find the list of students ` `# having maximum average score ` `def` `getStudentsList(``file``): ` `    `  `    ``# Variables to store maximum ` `    ``# average score ` `    ``maxAvgScore ``=` `0`   `    ``# List to store names of students ` `    ``# having maximum average score ` `    ``names ``=` `[] `   `    ``# Traversing the file data ` `    ``for` `i ``in` `range``(``0``, ``len``(``file``), ``4``):`   `        ``# finding average score ` `        ``# of a student ` `        ``avgScore ``=` `(``int``(``file``[i ``+` `1``]) ``+` `                    ``int``(``file``[i ``+` `2``]) ``+` `                    ``int``(``file``[i ``+` `3``])) ``/``/` `3`   `        ``if` `avgScore > maxAvgScore:` `            ``maxAvgScore ``=` `avgScore `   `            ``# Clear the list and add name ` `            ``# of student having current ` `            ``# maximum average score in the list ` `            ``names.clear() ` `            ``names.append(``file``[i]) ` `            `  `        ``elif` `avgScore ``=``=` `maxAvgScore: ` `            ``names.add(``file``[i]) ` `        `  `    ``# Printing the maximum average score and names ` `    ``# of students having this maximum average score ` `    ``# as per the order in the file. ` `    ``for` `i ``in` `range``(``len``(names)):` `        ``print``(names[i], end ``=` `" "``) ` `        `  `    ``print``(maxAvgScore) `   `# Driver Code` `if` `__name__ ``=``=` `"__main__"``:` `    `  `    ``file` `=` `[``"Shrikanth"``, ``"20"``, ``"30"``, ``"10"``, ` `            ``"Ram"``, ``"100"``, ``"50"``, ``"10"``] ` `                    `  `    ``getStudentsList(``file``) `   `# This code is contributed` `# by rituraj_jain`

## C#

 `// C# program to find the` `// list of students having maximum average score`   `using` `System;` `using` `System.Collections.Generic;` `class` `GFG {`   `    ``// Function to find the` `    ``// list of students having maximum average score` `    ``// Driver code` `    ``static` `void` `getStudentsList(``string` `[] file)` `    ``{` `        ``// Variables to store average score of a student` `        ``// and maximum average score` `        ``int` `avgScore;` `        ``int` `maxAvgScore = Int32.MinValue;`   `        ``// List to store names of students` `        ``// having maximum average score` `        ``List<``string``> names = ``new` `List<``string``>();`   `        ``// Traversing the file data` `        ``for` `(``int` `i = 0; i < file.Length; i += 4) {`   `            ``// finding average score of a student` `            ``avgScore = (Int32.Parse(file[i + 1]) + ` `                        ``Int32.Parse(file[i + 2]) + ` `                    ``Int32.Parse(file[i + 3])) / 3;`   `            ``if` `(avgScore > maxAvgScore) {` `                ``maxAvgScore = avgScore;`   `                ``// Clear the list and add name of student` `                ``// having current maximum average score in the list` `                ``names.Clear();` `                ``names.Add(file[i]);` `            ``}`   `            ``else` `if` `(avgScore == maxAvgScore)` `                ``names.Add(file[i]);` `        ``}`   `        ``// Printing the maximum average score and names` `        ``// of students having this maximum average score` `        ``// as per the order in the file.` `        ``for` `(``int` `i = 0; i < names.Count; i++) {` `            ``Console.Write(names[i] + ``" "``);` `        ``}`   `        ``Console.WriteLine(maxAvgScore);` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `Main()` `    ``{` `        ``string``[] file = { ``"Shrikanth"``, ``"20"``, ``"30"``, ` `                        ``"10"``, ``"Ram"``, ``"100"``, ``"50"``, ``"10"` `};` `        ``getStudentsList(file);` `    ``}` `}`     `// This code is contributed by ihritik`

## PHP

 ` ``\$maxAvgScore``) ` `        ``{` `            ``\$maxAvgScore` `= ``\$avgScore``;`   `            ``// Clear the list and add name of ` `            ``// student having current maximum` `            ``// average score in the list` `            ``unset(``\$names``);` `            ``\$names` `= ``array``();` `            ``array_push``(``\$names``, ``\$file``[``\$i``]);` `        ``}`   `        ``else` `if` `(``\$avgScore` `== ``\$maxAvgScore``)` `            ``array_push``(``\$names``, ``\$file``[``\$i``]);` `    ``}`   `    ``// Printing the maximum average score ` `    ``// and names of students having this ` `    ``// maximum average score as per the ` `    ``// order in the file.` `    ``for` `(``\$i` `= 0; ``\$i` `< ``count``(``\$names``); ``\$i``++) ` `    ``{` `        ``echo` `\$names``[``\$i``] . ``" "``;` `    ``}`   `    ``echo` `\$maxAvgScore``;` `}`   `// Driver code` `\$file` `= ``array``( ``"Shrikanth"``, ``"20"``, ``"30"``, ``"10"``, ` `               ``"Ram"``, ``"100"``, ``"50"``, ``"10"` `);`   `// Number of elements in string array         ` `\$n` `= ``count``(``\$file``);`   `getStudentsList(``\$file``, ``\$n``);`   `// This code is contributed by mits` `?>`

## Javascript

 ``

Output:

`Ram 53`

My Personal Notes arrow_drop_up
Recommended Articles
Page :