Students with maximum average score of three subjects | Function to find the list of students having maximum average score ; Variables to store average score of a student and maximum average score ; List to store names of students having maximum average score ; Traversing the file data ; finding average score of a student ; Clear the list and add name of student having current maximum average score in the list ; Printing the maximum average score and names of students having this maximum average score as per the order in the file . ; Driver code ; Number of elements in string array
< ? php function getStudentsList ( $ file , $ n ) { $ maxAvgScore = PHP_INT_MIN ; $ names = array ( ) ; $ avgScore = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i += 4 ) { $ avgScore = ( int ) ( ( intval ( $ file [ $ i + 1 ] ) + intval ( $ file [ $ i + 2 ] ) + intval ( $ file [ $ i + 3 ] ) ) / 3 ) ; if ( $ avgScore > $ maxAvgScore ) { $ maxAvgScore = $ avgScore ; unset ( $ names ) ; $ names = array ( ) ; array_push ( $ names , $ file [ $ i ] ) ; } else if ( $ avgScore == $ maxAvgScore ) array_push ( $ names , $ file [ $ i ] ) ; } for ( $ i = 0 ; $ i < count ( $ names ) ; $ i ++ ) { echo $ names [ $ i ] . " " ; } echo $ maxAvgScore ; } $ file = array ( " Shrikanth " , "20" , "30" , "10" , " Ram " , "100" , "50" , "10" ) ; $ n = count ( $ file ) ; getStudentsList ( $ file , $ n ) ; ? >