Build a program that takes an array of exam grades and prints the following statistics: the highest grade, the lowest grade, the class average, and how many students scored above the average.
Here is one approach:
import java.util.Arrays;
int[] grades = {72, 85, 90, 68, 95, 78, 88, 60};
Arrays.sort(grades);
int min = grades[0];
int max = grades[grades.length - 1];
double sum = 0;
for (int g : grades) {
sum += g;
}
double avg = sum / grades.length;
int aboveAvg = 0;
for (int g : grades) {
if (g > avg) {
aboveAvg++;
}
}
System.out.println("Min: " + min);
System.out.println("Max: " + max);
System.out.println("Avg: " + avg);
System.out.println("Above average: " + aboveAvg);
Sorting first lets you grab the min and max without a separate scan. The sum uses a double accumulator so the average calculation does not lose decimals.