Students with maximum average score of three subjects

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 :  
Traverse the file data and store average scores for each student.Now, find the maximum average score and search for all the students with this maximum average score.Print the maximum average score and names as per the order in the file.
Traverse the file data and store average scores for each student.
Now, find the maximum average score and search for all the students with this maximum average score.
Print the maximum average score and names as per the order in the file.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find the// list of students having maximum average score#include<bits/stdc++.h>using namespace std; // Function to find the// list of students having maximum average score// Driver codevoid 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<string> 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 <<names[i] + " ";    }     cout << maxAvgScore;} // Driver codeint main(){    string file[] = { "Shrikanth", "20", "30",                    "10", "Ram", "100", "50", "10" };         // Number of elements in string array                   int n= sizeof(file)/sizeof(file[0]);         getStudentsList(file,n);} // This code is contributed by ihritik
// 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<String> 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 program to find the list of# students having maximum average score # Function to find the list of students# having maximum average scoredef 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 Codeif __name__ == "__main__":         file = ["Shrikanth", "20", "30", "10",            "Ram", "100", "50", "10"]                         getStudentsList(file) # This code is contributed# by rituraj_jain
// 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// PHP program to find the list of students// having maximum average score // Function to find the list of students// having maximum average scorefunction getStudentsList($file, $n){    // Variables to store average score of    // a student and maximum average score    $maxAvgScore = PHP_INT_MIN;     // List to store names of students    // having maximum average score    $names = array();    $avgScore = 0;         // Traversing the file data    for ($i = 0; $i < $n; $i += 4)    {         // finding average score of a student        $avgScore = (int)((intval($file[$i + 1]) +                           intval($file[$i + 2]) +                           intval($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            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?>
<script>// Javascript program to find the// list of students having maximum average score // Function to find the// list of students having maximum average score// Driver codefunction getStudentsList(file, n){     // Variables to store average score of a student    // and maximum average score    let avgScore;    let maxAvgScore = Number.MIN_SAFE_INTEGER;     // List to store names of students    // having maximum average score    let names = [];     // Traversing the file data    for (let i = 0; i < n; i += 4)    {         // finding average score of a student        avgScore = Math.floor((Number(file[i + 1]) + Number(file[i + 2]) + Number(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 = [];            names.push(file[i]);        }         else if (avgScore == maxAvgScore)            names.push(file[i]);    }     // Printing the maximum average score and names    // of students having this maximum average score    // as per the order in the file.    for (let i = 0; i < names.length; i++) {        document.write(names[i] + " ");    }     document.write(maxAvgScore);} // Driver code let file = ["Shrikanth", "20", "30",    "10", "Ram", "100", "50", "10"]; // Number of elements in string array               let n = file.length; getStudentsList(file, n); // This code is contributed by gfgking. </script>
Ram 53
 
rituraj_jain
ihritik
Mithun Kumar
nidhi_biet
gfgking
array-traversal-question
Arrays
Java Programs
Strings
Arrays
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.