Question

In: Computer Science

Write a method that, given an array of grades stored as double values, computes and return...

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

Solutions

Expert Solution

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:


Related Solutions

Write a function that will take in an array (of type double), and will return the...
Write a function that will take in an array (of type double), and will return the array with all of the elements doubled. You must use pass-by-reference and addressing and/or pointers to accomplish this task. C++
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
Write recursive method to return true if a given array has element equal to employee emp,...
Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary, // and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp, and returns...
Assume that you have an array of 100 elements representing the grades students are stored in...
Assume that you have an array of 100 elements representing the grades students are stored in memory. Suppose the grades are in IEEE single precision format. Write a MIPS program to compute the average of the students grades and store the result in register $f0. Assume the array base address is stored in register $s0 and a floating point value of 100 is stored in memory with it address given in register $s2.
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
Write a Java method that computes the future investment value at a given interest rate for...
Write a Java method that computes the future investment value at a given interest rate for a specified number of years. Your method should return the future investment value after calculation. The future investment is determined using the formula below:     futureInvestmentValue = investmentAmount * (1+monthlyInterestRate)numberOfYears*12 You can use the following method header: public static double futureInvestmentValue (double investmentAmount, double monthlyInterestRate, int years);
Write a function double mysqrt( double x ) which computes the value of the square root...
Write a function double mysqrt( double x ) which computes the value of the square root of x using the bisection method. First you need to set the left and right bounds for x. If 0<x<1 then lt = x and rt = 1 and the sqrt(x) is somewhere in between. If x > 1 then lt = 1 and rt = x and sqrt(x) is somewhere in between. In a loop you need to compute the mid value between...
Write a method named area with one double parameter name radius. The method needs to return...
Write a method named area with one double parameter name radius. The method needs to return a double value that represents the area of a circle. If the parameter radius is negative , then return -1.0 to present an invalid value Write another overload method with 2 parameters side1 and side2 (both double) where side1 and side2 represent the sides of a rectangle. The method needs to return an area of a rectangle . If either or both parameters is/...
Write a method named area with one double parameter name radius, the method needs to return...
Write a method named area with one double parameter name radius, the method needs to return a double value that represents the area of a circle. If the parameter radius is negative, then return -1.0 to represents an invalid value write another overload method with 2 parameter side 1 and side 2 (double) where side 1 and side 2 represents the sides of a rectangle. The method needs to return an area of a rectangle. If either or both parameters...
Write a static method remove(int v, int[] in) that will return a new array of the...
Write a static method remove(int v, int[] in) that will return a new array of the integers in the given array, but with the value v removed. For example, if v is 3 and in contains 0, 1, 3, 2, 3, 0, 3, and 1, the method will return an array containing 0, 1, 2, 0, and 1. Hint: You can follow two steps to solve this problem: Create an array in the method, let say you called it result....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT