In: Computer Science
Write a method that, given an array of grades stored as double
values, computes and return their mean.
Write another method that will return an array of the
mean grade obtained by an arbitrary number of student.
This method will take a 2-dimensional array of double
values in which each row corresponds to the grade vector
for a different student. You can compute the mean for
each row as you did before...
Once you are done, be [good] lazy and just reuse your
previous method.
If you are looking for yet another practice
opportunity, what about a method that, given the whole
matrix,
would bump up a specific grade of a specific student
by a given number of points?
Signature of this method is not provided below.
*/
public class TinkeringWithArraysLive {
public static void main(String[] args){
double[] s1 = {90.1, 80.2, 70.3, 30.4, 99.5};
System.out.println("Computing average of grades " +
Arrays.toString(s1) + " result is " + mean(s1));
double[][] all = { {50.0, 60.0, 52.0, 58.0, 55.0},
{60.0, 70.0, 62.0, 68.0, 65.0},
{70.0, 80.0, 72.0, 78.0, 75.0}
};
System.out.println("Averages for all students are " +
Arrays.toString(mean(all)));
} // end main
public static double mean(double[] grades){
return 0.0;
}// end method mean for vector of grades
public static double[] mean(double[][] grades){
double[] result = new double[grades.length];
return result;
}// end method mean for matrix of grades
} // end class
Code:
import java.util.Arrays;
public class TinkeringWithArraysLive {
public static void main(String[] args)
{
double[] s1 = {90.1, 80.2, 70.3, 30.4, 99.5};
System.out.println("Computing average of grades " +
Arrays.toString(s1) + " result is " + mean(s1));
double[][] all = { {50.0, 60.0, 52.0, 58.0,
55.0},
{60.0, 70.0, 62.0, 68.0, 65.0},
{70.0, 80.0, 72.0, 78.0, 75.0}
};
System.out.println("Averages for all students are " +
Arrays.toString(mean(all)));
} // end main
public static double mean(double[] grades)
{
double total = 0.0;
for(int i = 0;i < grades.length;i++)
{
total += grades[i];
}
return total/grades.length;
}// end method mean for vector of grades
public static double[] mean(double[][] grades)
{
double[] result = new double[grades.length];
for(int j = 0;j < grades.length;j++)
{
double total = 0.0;
for(int i = 0;i <
grades[j].length;i++)
{
total +=
grades[j][i];
}
result[j] =
total/grades[j].length;
}
return result;
}// end method mean for matrix of grades
} // end class
OUTPUT: