Write a static method named matrixAverages that accepts a two-dimensional array
ID: 3699249 • Letter: W
Question
Write a static method named matrixAverages that accepts a two-dimensional array of integers as a parameter. The method averages each row and column of the matrix and prints out each of the averages.
A template .java file is provided (MatAvgTemplate.java) that will provide the class definition, method definition and several matrices that will be used for test cases.
The main() procedure should simply pass the matrix to matrixAverages which will do the computations AND print the results.
The prototype for matrixAverages is:
public static void matrixAverages(int[][] theMatrix);
Background
Recall that in row major order, the elements are arranged such that the right index advances the quickest. So the order of elements is for an array declared:
int[][] anArray = new int[2][3];
is
anArray[0][0], anArray[0][1], anArray[0][2]
anArray[1][0], anArray[1][1], anArray[1][2]
Averaging rows would involve adding the three elements in the top row and dividing by 3 followed by adding the elements in the 2nd row above and dividing by 3.
So…
(anArray[0][0] + anArray[0][1] + anArray[0][2]) / (double) 3
and…
(anArray[1][0] + anArray[1][1] + anArray[1][2]) / (double) 3
Averaging columns would involve adding pairs of elements that are above and below each other in the list above and dividing by 2.
So the averages would be:
(anArray[0][0] + anArray[1][0]) / (double) 2
(anArray[0][1] + anArray[1][1]) / (double) 2
(anArray[0][2] + anArray[1][2]) / (double) 2
The program must be generalized to handle any size 2 dimensional array.
Example Run
For the test array:
int[][] tmatrix1 = {{2, 10, 14, 200},
{8, 19, 30, 21},
{100, 1001, -4, 15},
{10, 20, -15, 25}};
The output should be:
Row 0 average is 56.5
Row 1 average is 19.5
Row 2 average is 278.0
Row 3 average is 10.0
Column 0 average is 30.0
Column 1 average is 262.5
Column 2 average is 6.25
Column 3 average is 65.25
Explanation / Answer
import java.util.Scanner;
public class MatrixAvgerage {
public static void MatrixAvg(int[][] thematrix,int rowsize,int columnsize){
for(int i=0;i<rowsize;i++){
float sum=0;
for(int j=0;j<columnsize;j++){
sum=sum+thematrix[i][j];
}
System.out.println("row"+i+"----->"+sum/columnsize);
}
for(int i=0;i<columnsize;i++){
float sum=0;
for(int j=0;j<rowsize;j++){
sum=sum+thematrix[j][i];
}
System.out.println("column"+i+"----->"+sum/rowsize);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int[][] tmatrix1 = new int[100][100];
System.out.println("Enter the rows that you want to bulid a Matrix...");
int row=sc.nextInt();
System.out.println("Enter the columns that you want to bulid a Matrix...");
int column=sc.nextInt();
System.out.println("Enter the Matrix...");
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
tmatrix1[i][j] =sc.nextInt();
}
}
MatrixAvg(tmatrix1,row,column);
}
}
Note : please the program with your requirement......if any doubts revert back me....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.