Search⌘ K

Solution Review: Find the Top Scorer in a Class

Explore how to identify the top scorer in a class by finding the maximum value in an array. Learn to create and traverse arrays using loops, compare values effectively, and return the maximum using a custom method in Java. This lesson helps you understand array handling and basic algorithm implementation.

Rubric criteria

Solution

Java
class Maximum
{
public static void main(String args[])
{
int[] array = {34, 2, 7, 12}; // Creating an array to store students' marks
System.out.println(Max(array)); // Passing the array to the Max function
}
// Function to find maximum value from array
public static int Max(int array[])
{
int max = 0; // Keeping track of max value
for(int i = 0; i < array.length; i++) // Traversing array till the end
{
if (max < array[i]) // If value is greater than max
{
max = array[i]; // Update max
}
}
return max;
}
}

Rubric-wise explanation


Point 1:

Look at line 10. We create the header of the Max function, which takes a parameter array of int[] type. We declare a variable, max, to ...