-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathMatrix Multiplication.java
66 lines (62 loc) · 1.24 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
import java.util.Scanner;
// Other imports go here, Do NOT change the class name
class Main
{
public static void main(String[] args)
{
Scanner obj=new Scanner(System.in);
int t=obj.nextInt();
for(int s=0;s<t;s++)
{
int r1=obj.nextInt();
int c1=obj.nextInt();
int a[][]=new int[r1][c1];
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
a[i][j]=obj.nextInt();
}
}
int r2=obj.nextInt();
int c2=obj.nextInt();
int b[][]=new int[r2][c2];
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
b[i][j]=obj.nextInt();
}
}
int c[][]=new int[r1][c2];
if(c1==r2)
{
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
for(int k=0;k<c1;k++)
{
c[i][j]=c[i][j] + a[i][k]*b[k][j];
}
}
}
}
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.print("\n");
}
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
c[i][j]=0;
}
}
}
}
}