Answer:
I am writing JAVA program:
import java.util.Scanner; // to take input from user
public class MatrixMultiplication {
public static void main(String[] args) { // start of main function
double[][] matrixA = new double[3][3]; // matrix 1
double[][] matrixB = new double[3][3]; // matrix 2
Scanner input = new Scanner(System.in); // scans user input
System.out.print("Enter matrix A: "); // prompts to enter matrix A
for (int i = 0; i < matrixA.length; i++) {
//loop to enter values in matrix A till length of matrixA
for (int j = 0; j < matrixA.length; j++)
// reads the matrixA values entered by the user
matrixA[i][j] = input.nextDouble(); }
System.out.print("Enter matrix B: "); // prompts to enter matrix B
//loop to enter values in matrix B till length of matrixB
for (int i = 0; i < matrixB.length; i++) {
for (int j = 0; j < matrixB.length; j++)
// reads the matrixB values entered by the user
matrixB[i][j] = input.nextDouble();}
//matrixC stores the result of the product of matrixA and matrixB
double[][] matrixC = Multiplication(matrixA, matrixB);
// Display function is called to display the matrices A, B and product //result which is matrixC
Display(matrixA, matrixB, matrixC); }
//function Multiplication to calculate the product of matrixA and matrixB
public static double[][] Multiplication(double[][] inputMatrix1, double[][] inputMatrix2) {
//stores the product of two matrices
double[][] outputMatrix = new double[3][3];
//the loops below are used to move through the rows of outputMatrix
for (int i = 0; i < outputMatrix.length; i++) {
for (int j = 0; j < outputMatrix[i].length; j++) {
for (int k = 0; k < outputMatrix.length; k++) {
/* takes the product of inputMatrix1 and inputMatrix2 and adds result of product of each row and stores the result in matrix outputMatrix*/
outputMatrix[i][j] += inputMatrix1[i][k] * inputMatrix2[k][j];
} } }
return outputMatrix; } //returns the resultant matrix
public static void resultMatrix(double[][] matrix, int row) {
//displays all the values in row form
for (int j = 0; j < matrix[row].length; j++) {
System.out.printf("%.1f", matrix[row][j]); } }
public static void Display(double[][] matrix1, double[][] matrix2, double[][] matrix3) { //displays the result of product of two matrices
System.out.println("The product of two 3*3 matrices is: ");
for (int i = 0; i < 3; i++) {
resultMatrix(matrix1, i); //displays elements of matrix A
System.out.print((i == 1 ? " * " : " "));
resultMatrix(matrix2, i); //displays elements of matrix B
System.out.print((i == 1 ? " = " : " "));
resultMatrix(matrix3, i); //displays elements of resultant matrixC
System.out.println(); } } }
Step-by-step explanation:
The program has three functions and main() function. In the main() function programs prompts the user to enter the elements of matrix A and matrix B and uses Scanner class to take this input from the user. Then Multiplication() function is called which computes the product of the two 3*3 matrices i.e. matrixA and matrixB and stores the result in matrixC. Next Display() function is called which displays the elements of matrix A, matrix B and the resultant matrix C in the matrix form with * between matrix A and B and the = sign between the input matrices and their product matrix.
The output of the program is attached as screen shot.