Write a Java program that asks the users to enter a m × n matrix of integers, m
ID: 3829569 • Letter: W
Question
Write a Java program that asks the users to enter a m × 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 × n array.
Then, write a class with the following three methods:
Method 1 accepts the m × n array as its argument, performs its transpose, and returns the transposed n × 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:
20 -5 90 22 32 34 29 -3 44 2 100 0 92 37 0
The program should output:
The transpose is: 20 34 100 -5 29 0 90 -3 92 22 44 37 32 2 0
The maximum of all the column elements: 100 29 92 44 32
The average of all the row elements: 31.8 21.2 45.8
Explanation / Answer
import java.io.*;
import java.util.*;
class Matrix{
public int[][] transpose(int arr[][]){
int m = arr.length;
int n = arr[0].length;
int ans[][] = new int[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
ans[i][j] = arr[j][i];
}
}
return ans;
}
public int maxCol(int arr[][],int col){
int ans = Integer.MIN_VALUE;
for(int i=0;i<arr.length;i++){
ans = Math.max(ans, arr[i][col-1]);
}
return ans;
}
public double avgRow(int arr[][], int row){
double sum = 0;
for(int j=0;j<arr[0].length;j++){
sum = sum + arr[row-1][j];
}
return sum/arr[0].length;
}
public void toString(int arr[][]){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[0].length;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println("");
}
}
}
public class Sample1 {
public static void main(String[] args) {
System.out.println("enter m and n");
int m,n;
Scanner obj = new Scanner(System.in);
m = obj.nextInt();
n = obj.nextInt();
int arr[][] = new int[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
arr[i][j] = obj.nextInt();
}
}
Matrix mat = new Matrix();
int trans[][] = mat.transpose(arr);
System.out.println("Transpose is ");
mat.toString(trans);
System.out.print("Maximum of all the column elements : ");
for(int i=1;i<=n;i++){
System.out.print(mat.maxCol(arr, i)+" ");
}
System.out.println("");
System.out.println("Average of all the row elements : ");
for(int i=1;i<=m;i++){
System.out.print(mat.avgRow(arr, i)+" ");
}
System.out.println("");
}
}
OUTPUT :
enter m and n
3 5
20 -5 90 22 32
34 29 -3 44 2
100 0 92 37 0
Transpose is
20 34 100
-5 29 0
90 -3 92
22 44 37
32 2 0
Maximum of all the column elements : 100 29 92 44 32
Average of all the row elements :
31.8 21.2 45.8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.