At the comment step 4 I am trying to add the current array element to sum, compare the current array element to max_test and if it is larger, save it in the variable max_test. and compare the current element to min_test, if it is smaller save it in min_test. HOWEVER i keep on getting the errors
Grades5.java:55: error: bad operand types for binary operator '>'
if (grades[r] > grades[max_test])
^
first type: int[]
second type: int[]
Grades5.java:57: error: bad operand types for binary operator '<'
if (grades[r] < grades[min_test])
^
first type: int[]
second type: int[]
Grades5.java:59: error: bad operand types for binary operator '+'
sum += grades[r];
^
first type: int
second type: int[]
3 errors
The code:
import java.util.Scanner;
public class Grades5
{
public static void main(String[] args)
{
int[][] grades = {
{ 87, 96, 100},
{ 68, 75, 72},
{ 99, 100, 95},
{100, 96, 70},
{ 75, 60, 79},
};
int how_many_grades = grades.length * grades[0].length;
// -----------------
// Output the grades
// -----------------
System.out.print(" ");
for (int i = 0; i < grades[0].length; i++)
System.out.print("Test " + (i + 1) + " ");
System.out.println("Average");
for (int r = 0; r < grades.length; r++)
{
int sum = 0; // Sum of one student's tests
// -------------------
// Process one student
// -------------------
System.out.print("Student " + (r + 1) + " ");
for (int c = 0; c < grades[r].length; c++)
{
System.out.printf("%6d ", grades[r]); // Step 1
//sum += grades[c]; // Step 2
}
System.out.printf("%7.2f\n", (double)sum / grades[r].length);
}
// ----------------
// Output a summary
// ----------------
int max_test, // Maximum test score
min_test, // Minimum test score
sum = 0; // Sum of all student tests
max_test = min_test = grades[0][0]; // Step 3
for (int r = 0; r < grades.length; r++)
{
// -------------------
// Process one student
// -------------------
for (int c = 0; c < grades[r].length; c++)
{
// Step 4
if (grades[r] > grades[max_test])
max_test = c;
if (grades[r] < grades[min_test])
min_test = c;
sum += grades[r];
}
}
System.out.println("Highest test score: " + max_test);
System.out.println("Lowest test score: " + min_test);
System.out.printf("Average test score: %.1f\n",
(double)sum / how_many_grades);
}
}
grades[r]andgrades[max_test]are, and what that means from your error message. It says you are trying to doint[] > int[], essentially compare two integer arrays, which is probably not what you intended to do.