# 2D Array Average of each row and ignoring single values

Hey,
I’ve got an assignment about 2D Arrays in which the array is supposed to be an ozon meter and the values are hourly measurements per day, with the value = 0 meaning the meter was defect.
We need to get the average, minimum and maximum of each day and ignore the defect values.

So far my code looks like this:

``````int[][] measurements = {
{7, 40, 42, 45, 51, 59, 67, 110, 0, 150, 190, 209, 232, 245, 250, 249, 240, 230, 208, 180, 160, 110, 89, 70 },
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1}
};
float sum = 0;
float defect = 0;
for (int day= 0; day < measurements.length; day++)
{
println("Measurements on day: " + (day+1)+ ": ");

for (int hour= 0; hour < measurements[day].length; hour++)
{
sum= (float) sum+measurements [day][hour];
print (measurements[day][hour] + " ");
}
float average = (float) sum/measurements[day].length;
float min = min (measurements[day]);
float max = max (measurements[day]);
println();
println ("Daily Average: " +average);
if (max > defect) {
println ("Max: " +max);
} else {
println ("Defect Meter");
}
if (min > defect) {
println ("Min: " +min);
}
}

``````

With the print being this:

``````Measurements on day: 1:
7 40 42 45 51 59 67 110 0 150 190 209 232 245 250 249 240 230 208 180 160 110 89 70
Daily Average: 134.70833
Max: 250.0
Measurements on day: 2:
0 0 0 0 0 0 0 0
Daily Average: 404.125
Defect Meter
Measurements on day: 3:
0 1
Daily Average: 1617.0
Max: 1.0
``````

1.) Obviously the daily average is completly wrong.On the first day there’s one 0 that messes it up aswell as the other two days being completly false. I figure I have to somehow set the value of “average” to 0 after each day, but don’t know how.

2.) On the first day the Min should be 7, but due the aforementioned 0 it doesn’t show a minimum for that day (if there are any lines of code that are unnecessarily complicated, I’m just a beginner and have basically no idea what I’m doing, so please tell me).

3.)There’s another part to the assignment in which we have to draw a bar chart out of this with defect values being shown differently and values over 180 in red. But that we got another week and never actually talked about this so I guess we will do this (in part) in class.

1 Like

1. point

Imho:

This

`````` sum= (float) sum+measurements [day][hour];
``````

won’t work.
You need to check if the value is != 0 before adding (and we need this if-clause later).

Similarly

`````` float average = (float) sum/measurements[day].length;
``````

won’t work since

``````measurements[day].length;
``````

is too high. It includes the zero measurements.

Instead after this line (but inside the if-clause!)

`sum= (float) sum+measurements [day][hour];`

count the valid measurements: `validMeasurements++;`

And use validMeasurements in the average formula.

Point 2

`````` float min = min (measurements[day]);
``````

is of course wrong and will return zero.

after `sum= (float) sum+measurements [day][hour];`
check if the value measurements [day][hour]; is smaller than your own minValue and if so

`minValue = measurements [day][hour];`

Chrisir

1 Like

I do understand the theory behind what I have (using if to check if any value is 0), but whenever I try something like

``````if (measurements [day] !=0) {
sum= (float) sum+measurements [day][hour];

print (measurements[day][hour] + " ");
}
``````

I get

“Incompatible operand types int[] and int”

and have no idea what to do about that.

``````measurements [day][hour] != 0
``````