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

Write a Java program that asks the users to enter a m × n matrix of integers, m

ID: 3833305 • 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

Below is your code: -

Matrix.java


import java.util.Scanner;

public class Matrix {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the number of rows of matrix:");
       int m = Integer.parseInt(sc.nextLine());
       System.out.println("Enter the number of columns of matrix:");
       int n = Integer.parseInt(sc.nextLine());

       int[][] arr = new int[m][n];
       System.out.println("Enter the values of the matrix row wise.");

       for (int i = 0; i < m; i++) {
           for (int j = 0; j < n; j++) {
               arr[i][j] = Integer.parseInt(sc.nextLine());
           }
       }
       sc.close();
       System.out.println();
       System.out.println("Your entered matrix is : ");

       for (int i = 0; i < m; i++) {
           for (int j = 0; j < n; j++) {
               System.out.print(arr[i][j] + " ");
           }
           System.out.println();
       }
       Matrix mat = new Matrix();
       int[][] transposedMatrix = mat.getTranspose(arr);

       System.out.println();
       System.out.println("Your Transposed matrix is : ");

       for (int i = 0; i < n; i++) {
           for (int j = 0; j < m; j++) {
               System.out.print(transposedMatrix[i][j] + " ");
           }
           System.out.println();
       }
       System.out.println();

       int[] maxCols = new int[n];

       int c = 0;
       for (int i = 0; i < n; i++) {
           maxCols[c++] = mat.getMaxOfColumn(transposedMatrix[i]);
           // to get columns we can use transposed matrix and get its rows
       }
       System.out.println("The maximum of all the column elements:");
       for (int i = 0; i < n; i++) {
           System.out.print(maxCols[i] + " ");
       }
      
       System.out.println();
      
       float[] averageRows = new float[m];

       c = 0;
       for (int i = 0; i < m; i++) {
           averageRows[c++] = mat.getAverage(arr[i]);
           // to get columns we can use transposed matrix and get its rows
       }
       System.out.println("The average of all the rows elements:");
       for (int i = 0; i < m; i++) {
           System.out.print(averageRows[i] + " ");
       }
      
   }

   public int[][] getTranspose(int[][] arr) {
       int m = arr.length; // getting dimensions of the original matrix
       int n = arr[0].length;

       int[][] trasposedMatrix = new int[n][m]; // initiating a
                                                   // transposedMatrix having
                                                   // dimensions interchanged

       for (int x = 0; x < n; x++) {
           for (int y = 0; y < m; y++) {
               trasposedMatrix[x][y] = arr[y][x]; // setting the column
                                                   // values as rows.
           }
       }

       return trasposedMatrix;
   }

   public int getMaxOfColumn(int[] arr) {
       int max = arr[0];// Initially setting first element as maximum

       for (int i = 1; i < arr.length; i++) {
           if (arr[i] > max) {// if any of the value in array is maximum, then
                               // set max to that value
               max = arr[i];
           }
       }
       return max;
   }

   public float getAverage(int[] arr) {
       int sum = 0;
       // getting the sum of elements of the array
       for (int i = 0; i < arr.length; i++) {
           sum = sum + arr[i];
       }
       // returning average by deviding sum with number of elements.
       return sum / (float) arr.length;
   }

}

Sample Run: -

Enter the number of rows of matrix:
3
Enter the number of columns of matrix:
5
Enter the values of the matrix row wise.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Your entered matrix is :
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

Your Transposed matrix is :
1 6 11
2 7 12
3 8 13
4 9 14
5 10 15

The maximum of all the column elements:
11 12 13 14 15
The average of all the rows elements:
3.0 8.0 13.0

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