-
Notifications
You must be signed in to change notification settings - Fork 9
/
matrix Multiplication.java
86 lines (76 loc) · 2.34 KB
/
matrix Multiplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
MatrixMultiplication code helps in taking input as 2 matrices of any order and prints their product.
*/
import java.io.*;
import java.util.*;
public class matrixMultiplication{
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
// Taking input as matrix1:
System.out.println("Enter the no. of rows of the first matrix:");
int row1 = s.nextInt();
System.out.println("Enter the no. of cols of the first matrix:");
int col1 = s.nextInt();
int[][] mat1 = new int[row1][col1];
System.out.println("Enter the elements of the first matrix:");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
mat1[i][j] = s.nextInt();
}
}
// Taking input as matrix2:
System.out.println("Enter the no. of rows of the second matrix:");
int row2 = s.nextInt();
System.out.println("Enter the no. of cols of the second matrix:");
int col2 = s.nextInt();
int[][] mat2 = new int[row2][col2];
System.out.println("Enter the elements of the second matrix:");
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
mat2[i][j] = s.nextInt();
}
}
if (col1 == row2) {
int ans[][] = matrixMul(mat1, mat2);
display(ans);
} else System.out.print("Invalid input");
s.close();
}
public static int[][] matrixMul(int[][] arr1, int[][] arr2) {
int[][] res = new int[arr1.length][arr2[0].length];
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2[0].length; j++) {
res[i][j] = 0;
for (int k = 0; k < arr1[0].length; k++) {
res[i][j] += arr1[i][k] * arr2[k][j];
}
}
}
return res;
}
public static void display(int [][] mat){
for (int i=0;i<mat.length;i++){
for (int j=0;j<mat[0].length;j++){
System.out.print(mat[i][j] +" ");
}
System.out.println();
}
}
}
/*
Sample Input/Output:
Input:
2
2
1 2
3 4
2
2
5 6
7 8
Output:
19 22
43 50
Time Complexity:O(n^2)
Space Complexity:O(n)
*/