My requirements are as follows:
- use main method
- use the following array-
int[] grades = new int[] {
82, 70, 99, 90, 92, 75, 87, 85, 91, 100, 91, 87
};
Write code to step through the array of grades and calculate the average grade. Modify your code so that while it is stepping through the grades, it also looks for the minimum grade. Modify your code so that while it is stepping through the grades, it also looks for the maximum grade.
You should only have one loop and work on finding the average, min, and max on one sweep through the loop.Format your output so that it looks like this:
min: 70 max: 100
average: 87.4
----------------------------------This is what I have------------------------------------
package com.abc.array;
public class ArrayAverage {
public static void main(String[] args) {
int sum = 0;
int max = 0;
int min = 0;
// Creating array of grade values
int[] grades = new int[] {
82, 70, 99, 90, 92, 75, 87, 85, 91, 100, 91, 87
};
for (int i = 0; i < grades.length; i++) {
sum += grades[i];
if (min > grades[i]) {
min = grades[i];
}
if (max < grades[i]) {
max = grades[i];
}
}
double average = (double) sum / grades.length;
System.out.printf(" min: " + min);
System.out.printf("%n max: " + max);
System.out.printf("%naverage: %.1f", average);
}
}