Write a Java program that asks the users to enter a m times n matrix of integers
ID: 3835506 • Letter: W
Question
Write a Java program that asks the users to enter a m times n matrix of integers, m and n being the number of rows and columns, respectively. The program stores this matrix into a two-dimensional m times n array. Then, write a class with the following three methods: Method 1 accepts the m times n array as its argument, performs its transpose, and returns the transposed n times m array; Method 2 accepts the jth column of the array as its argument, and returns the maximum of the column elements; Method 3 accepts the ith row of the array as its argument, and returns the average of the row elements. Use this class in the main program to find the transpose, the maximum of column elements, and the average of row elements. The program should display all three results as shown below. For example, if the user enters the following content: The program should output: The transpose is: The maximum of all the column elements: The average of all the row elements:Explanation / Answer
Please find my implementation:
import java.util.Scanner;
public class MatrixTranspose {
public static int[][] trasposeMatrix(int[][] matrix)
{
int m = matrix.length;
int n = matrix[0].length;
int[][] trasposedMatrix = new int[n][m];
for(int x = 0; x < n; x++)
{
for(int y = 0; y < m; y++)
{
trasposedMatrix[x][y] = matrix[y][x];
}
}
return trasposedMatrix;
}
public static int maxOfColumn(int[][] matrix, int col){
int max = matrix[0][col];
for(int row=1; row<matrix.length; row++)
if(max < matrix[row][col])
max = matrix[row][col];
return max;
}
public static double[] averageOfRows(int[][] matrix){
double average[] = new double[matrix.length];
for(int i=0; i<matrix.length; i++){
double sum = 0;
for(int j=0; j<matrix[i].length; j++)
sum += matrix[i][j];
average[i] = sum/matrix[i].length;
}
return average;
}
public static void printMatric(int[][] matrix){
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix[i].length; j++)
System.out.println(matrix[i][j]+" ");
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Number of rows ? ");
int m = sc.nextInt();
System.out.print("Number of columns ? ");
int n = sc.nextInt();
int[][] matrix = new int[m][n];
for(int i=0; i<m; i++){
System.out.println("Enter "+n+" integers for row #"+(i+1)+": ");
for(int j=0; j<n; j++)
matrix[i][j] = sc.nextInt();
}
System.out.println("Matrix is: ");
printMatric(matrix);
System.out.println("Transpose of matric: ");
int[][] transpose = trasposeMatrix(matrix);
printMatric(transpose);
System.out.print("Enter column number( 0 "+(n-1)+"): ");
int col = sc.nextInt();
int max = maxOfColumn(matrix, col);
System.out.println("Max of column "+col+" is: "+max);
double[] avg = averageOfRows(matrix);
for(int i=0; i<avg.length; i++){
System.out.print("Average of row "+(i+1)+" is: "+avg[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.