Problem:
Java Code for Matrix Multiplication
Solution:
package com.basics;
import java.util.Scanner;
public class MatrixMultiplication {
static int[][] array1;
static int[][] array2;
static int[][] array3;
int rowLimit;
int columnLimit;
public static void main(String[] args) {
MatrixMultiplication matrixMultiplication = new MatrixMultiplication();
matrixMultiplication.initializeTheArrays();
System.out.println("the first matrix elements are: ");
matrixMultiplication.printMatrix(array1);
System.out.println("the second matrix elements are: ");
matrixMultiplication.printMatrix(array2);
matrixMultiplication.doMatrixMultiplication();
System.out.println("the resulted matrix elements are: ");
matrixMultiplication.printMatrix(array3);
}
public void initializeTheArrays(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the array row limit");
rowLimit = scanner.nextInt();
System.out.println("enter the array column limit");
columnLimit = scanner.nextInt();
array1 = new int[rowLimit][columnLimit];
System.out.println("enter the array1 elements");
for (int i = 0; i < rowLimit; i++) {
for (int j = 0; j < columnLimit; j++) {
System.out.println("enter the element");
array1[i][j] = scanner.nextInt();
}
}
array2 = new int[rowLimit][columnLimit];
System.out.println("enter the array2 elements");
for (int i = 0; i < rowLimit; i++) {
for (int j = 0; j < columnLimit; j++) {
System.out.println("enter the element");
array2[i][j] = scanner.nextInt();
}
}
array3 = new int[rowLimit][columnLimit];
scanner.close();
}
public void doMatrixMultiplication(){
for (int i = 0; i < rowLimit; i++) {
for (int j = 0; j < columnLimit; j++) {
for (int k = 0; k < rowLimit; k++)
{
array3[i][j] = array3[i][j] + array1[i][k] * array2[k][j];
}
}
}
}
public void printMatrix(int[][] array){
for (int i = 0; i < rowLimit; i++) {
for (int j = 0; j < columnLimit; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
Output:
enter the array row limit
2
enter the array column limit
2
enter the array1 elements
enter the element
1
enter the element
2
enter the element
3
enter the element
4
enter the array2 elements
enter the element
4
enter the element
3
enter the element
2
enter the element
1
the first matrix elements are:
1 2
3 4
the second matrix elements are:
4 3
2 1
the resulted matrix elements are:
8 5
20 13
Java Code for Matrix Multiplication
Solution:
package com.basics;
import java.util.Scanner;
public class MatrixMultiplication {
static int[][] array1;
static int[][] array2;
static int[][] array3;
int rowLimit;
int columnLimit;
public static void main(String[] args) {
MatrixMultiplication matrixMultiplication = new MatrixMultiplication();
matrixMultiplication.initializeTheArrays();
System.out.println("the first matrix elements are: ");
matrixMultiplication.printMatrix(array1);
System.out.println("the second matrix elements are: ");
matrixMultiplication.printMatrix(array2);
matrixMultiplication.doMatrixMultiplication();
System.out.println("the resulted matrix elements are: ");
matrixMultiplication.printMatrix(array3);
}
public void initializeTheArrays(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the array row limit");
rowLimit = scanner.nextInt();
System.out.println("enter the array column limit");
columnLimit = scanner.nextInt();
array1 = new int[rowLimit][columnLimit];
System.out.println("enter the array1 elements");
for (int i = 0; i < rowLimit; i++) {
for (int j = 0; j < columnLimit; j++) {
System.out.println("enter the element");
array1[i][j] = scanner.nextInt();
}
}
array2 = new int[rowLimit][columnLimit];
System.out.println("enter the array2 elements");
for (int i = 0; i < rowLimit; i++) {
for (int j = 0; j < columnLimit; j++) {
System.out.println("enter the element");
array2[i][j] = scanner.nextInt();
}
}
array3 = new int[rowLimit][columnLimit];
scanner.close();
}
public void doMatrixMultiplication(){
for (int i = 0; i < rowLimit; i++) {
for (int j = 0; j < columnLimit; j++) {
for (int k = 0; k < rowLimit; k++)
{
array3[i][j] = array3[i][j] + array1[i][k] * array2[k][j];
}
}
}
}
public void printMatrix(int[][] array){
for (int i = 0; i < rowLimit; i++) {
for (int j = 0; j < columnLimit; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
Output:
enter the array row limit
2
enter the array column limit
2
enter the array1 elements
enter the element
1
enter the element
2
enter the element
3
enter the element
4
enter the array2 elements
enter the element
4
enter the element
3
enter the element
2
enter the element
1
the first matrix elements are:
1 2
3 4
the second matrix elements are:
4 3
2 1
the resulted matrix elements are:
8 5
20 13
enter the array row limit
3
enter the array column limit
3
enter the array1 elements
enter the element
1
enter the element
2
enter the element
3
enter the element
4
enter the element
5
enter the element
6
enter the element
7
enter the element
8
enter the element
9
enter the array2 elements
enter the element
2
enter the element
3
enter the element
4
enter the element
5
enter the element
6
enter the element
7
enter the element
8
enter the element
9
enter the element
1
the first matrix elements are:
1 2 3
4 5 6
7 8 9
the second matrix elements are:
2 3 4
5 6 7
8 9 1
the resulted matrix elements are:
36 42 21
81 96 57
126 150 93