Reputation: 173
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);
}
}
Upvotes: 1
Views: 3447
Reputation: 49606
You cannot use operator >
to compare int[]
types. Java doesn't support operator overloading. Those relational operators (>
, <
, >=
, <=
) are applied to numeric primitive data types only.
Do you mean something like grades[r][c] > grades[r][max_test]
or grades[r][c] < grades[r][min_test]
?
Upvotes: 1
Reputation: 173
This works because grades needs 2 parts when comparing
if (grades[r][c] > max_test)
max_test = grades[r][c];
if (grades[r][c] < min_test)
min_test = grades[r][c];
sum += grades[r][c];
Upvotes: 0
Reputation: 191710
If the grades for one test are horizontal in your array, then you only need two loops, not three.
for (int test = 0; test < grades.length; test++) {
System.out.print("Test " + (test + 1) + " ");
System.out.println("Average");
int sum = 0; // Sum all grades on this test
for (int student = 0; student < grades[test].length; student++)
{
System.out.print("Student " + (student + 1) + " ");
sum += grades[student];
}
System.out.println();
System.out.printf("%7.2f\n", (double)sum / grades[test].length);
}
Upvotes: 1
Reputation: 25387
Your problem is that grades[r]
is not an integer. It is an array
of integers. You'd have to address two indices
sum += grades[i][j];
in order to get rid of the compilation error.
It appears to be the same for the other errors. In general you can imagine it as follows:
grades[1] -> { 87, 96, 100}
grades[2] -> { 68, 75, 72}
and
grades[1][1] -> 87;
grades[1][2] -> 96;
etc ..
Upvotes: 1