-
Notifications
You must be signed in to change notification settings - Fork 370
/
MatrixMultiplication.java
72 lines (46 loc) · 1.29 KB
/
MatrixMultiplication.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
import java.util.Scanner;
public class MatrixMultiplication {
public static void Display(int [][] a)
{
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int r1 = scn.nextInt();
int c1 = scn.nextInt();
int [][] oneArray = new int[r1][c1];
for (int i = 0; i < oneArray.length; i++) {
for (int j = 0; j < oneArray[0].length; j++) {
oneArray[i][j] = scn.nextInt();
}
}
int r2 = scn.nextInt();
int c2 = scn.nextInt();
int [][] twoArray = new int[r2][c2];
for (int i = 0; i < twoArray.length; i++) {
for (int j = 0; j < twoArray[0].length; j++) {
twoArray[i][j] = scn.nextInt();
}
}
if(c1 != r2)
{
System.out.println("Invalid input");
return;
}
int [][] prdArray = new int[r1][c2];
for (int i = 0; i < prdArray.length; i++) {
for (int j = 0; j < prdArray[0].length; j++) {
for(int k = 0; k<c1 ; k++)
{
prdArray[i][j] = prdArray[i][j] + oneArray[i][k] * twoArray[k][j];
}
}
}
Display(prdArray);
}
}