I'm having trouble getting the output to match the given output. I have the structure made but finding little discrepancies with my output.
public void printStudentsAvgWithoutLowest() {
//do not declare variable here
double average = 0;
for (int i = 0; i < arrScores.length; i ) {
double lowest = 100; //CHANGE HERE
for (int j = 0; j < arrScores[i].length; j ) {
if (arrScores[i].length < lowest) {
lowest = arrScores[i][j];
}
}
for (int k = 0; k < arrScores[i].length; k ) {
if (arrScores[i][k] != lowest) {
average = average arrScores[i][k];
}
}
average = average / (arrScores[i].length - 1);
System.out.printf("Student#%s Average (without lowest score): %.2f\n", i, average);
average = 0;
}
System.out.printf("\n");
}
this is the array I have made using txt file given.
100.00 90.00 100.00 80.00 70.00
50.00 60.00 70.00 80.00 100.00
60.00 70.00 100.00 80.00 90.00
69.50 70.50 80.50 30.50 0.00
78.30 69.50 48.00 90.00 100.00
88.50 95.00 100.00 99.00 0.00
I need to find the average of each row which excludes the lowest value.
my output is as follows:
Student#0 Average (without lowest score): 92.50
Student#1 Average (without lowest score): 65.00
Student#2 Average (without lowest score): 77.50
Student#3 Average (without lowest score): 62.75
Student#4 Average (without lowest score): 96.45
Student#5 Average (without lowest score): 95.63
sample output given:
Student#0 Average (without lowest score): 92.50
Student#1 Average (without lowest score): 77.50
Student#2 Average (without lowest score): 85.00
Student#3 Average (without lowest score): 62.75
Student#4 Average (without lowest score): 84.45
Student#5 Average (without lowest score): 95.63
Student# 1, 2, and 4 have different outputs given from the sample output.
CodePudding user response:
Each row can have a different lowest value, so you should define the variable inside the loop. (Additionally, although it does not come up in your current input, you might want to consider the case where the lowest value occurs multiple times. To solve that, you could sum all the array values while finding the minimum value, then subtract that minimum value from the computed sum before dividing.)
double lowest = Double.MAX_VALUE;
for (int j = 0; j < arrScores[i].length; j ) {
lowest = Math.min(lowest, arrScores[i][j]);
}