Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

File RegionAverage.java (shown above) contains an incomplete program. The goal o

ID: 3933387 • Letter: F

Question

File RegionAverage.java (shown above) contains an incomplete program. The goal of the program is to compute the average of values in specific regions of a matrix. Complete that program, by defining a regionAverage function, that satisfies the following specs:

Function regionAverage takes five arguments, called A, top, bottom, left, right. A is a 2D array of double numbers. Arguments top, bottom, left, right are all integers.

The function should return the average of values in all positions (i, j) of A such that top <= i <= bottom and left <= j <= right.

IMPORTANT: You are NOT allowed to modify in any way the main function. You are free to define and use auxiliary functions.

The complete program should produce this output:

Explanation / Answer

Code:

public class Main{
public static void printDoubleMatrix(String name, double[][] a) {
System.out.printf("%s: ", name);
for (int i = 0; i < a.length; i++){
for (int j = 0; j < a[i].length; j++){
System.out.printf("%7.1f", a[i][j]);
}
System.out.printf(" ");
}
System.out.printf(" ");
}
  
public static double regionAverage(double[][] a, int top, int bottom, int left, int right){
   double sum = 0;
   int n = 0;
   for(int i=top; i <= bottom; i++){
       for(int j = left; j <= right; j++){
           sum = sum + a[i][j];
           n = n+1;
       }
      
   }
   return sum/(double)n;
}
  
public static void main(String[] args){
double[][] a = { {3.2, 2.1, 5.3},
{8.0, 4.9, 5.7},
{18.0, 14.9, 15.7},
{28.0, 24.9, 25.7},
{38.0, 34.9, 35.7} };
  
printDoubleMatrix("a", a);
double result = regionAverage(a, 0, 3, 1, 2);
System.out.printf("regionAverage(a, 0, 3, 1, 2) returned %.2f. ", result);
result = regionAverage(a, 2, 4, 0, 2);
System.out.printf("regionAverage(a, 2, 4, 0, 2) returned %.2f. ", result);
}
}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote