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.
Thanks for your help!