[ 작업 환경 ]

 

Windows10

jdk-15

 

 

 

 

 

 [ 문제 ]

 

Write a program (TransposeMatrixMult.java) that asks the user to enter 9 integers 
and place them into a 3x3 2d array.

Then transpose the array so the first row becomes the first column, 
the second row becomes the second column, and the third row becomes the third column.

Include the following static function to perform the matrix multiplication of two matrices, 
first and second.

public static int[][] multiplication(int[][] first, int[][] second)
Display the original matrix, and the transposed matrix, and the multiplication of the two. 
Refer to the following console.

C:\HW7>java TransposeMatrixMult
Enter 9 integers by entering the Return key between each: 1
2
3
4
5
6
7
8
9
Original Matrix:
1 2 3
4 5 6
7 8 9
Transposed Matrix:
1 4 7
2 5 8
3 6 9
Transposed Matrix * Original Matrix:
66 78 90
78 93 108
90 108 126

 

 

 

 

 

1. TransposeMatrixMult.java 작성

 

import java.util.Scanner;
public class TransposeMatrixMult {
  public static void main (String[]args) {
    int i, j, k;
    int original_martix[][] = new int[3][3];
    int transposed_matrix[][] = new int[3][3];
    int final_matrix[][] = new int[3][3];
    System.out.print("Enter 9 integers by entering the Return key between each:" );
    Scanner scan = new Scanner (System.in);
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++)
          {
            original_martix[i][j] = scan.nextInt ();
          }
      }
      System.out.println("Original Matrix:");
      for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            
            System.out.print(original_martix[i][j] + " ");
          }
        System.out.println();
      }
      System.out.println("Transposed Matrix:");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            transposed_matrix[i][j] = original_martix[j][i];
            System.out.print(transposed_matrix[i][j] + " ");
          }
        System.out.println();
      }
    System.out.println("Transposed Matrix * Original Matrix:");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            final_matrix[i][j] = 0;
            for (k = 0; k < 3; k++) {
                final_matrix[i][j] += transposed_matrix[i][k] * original_martix[k][j];
              }
            System.out.print(final_matrix[i][j] + " ");
          }
        System.out.println();
    }
  }
}

 

 

 

 

 

2. 소스 실행 예시

 

javac TransposeMatrixMult.java
java TransposeMatrixMult

 

 

 

+ Recent posts