-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix Multiplication.java
More file actions
66 lines (57 loc) · 1.52 KB
/
Copy pathMatrix Multiplication.java
File metadata and controls
66 lines (57 loc) · 1.52 KB
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.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws Exception {
// write your code here
Scanner scn = new Scanner(System.in);
// Declaration & Initialization
int n1 = scn.nextInt();
int m1 = scn.nextInt();
int[][] arr1 = new int[n1][m1];
// INPUT: 1st
for(int rows = 0; rows <= n1-1; rows++)
{
for(int cols = 0; cols <= m1-1; cols++)
{
arr1[rows][cols] = scn.nextInt();
}
}
int n2 = scn.nextInt();
int m2 = scn.nextInt();
int[][] arr2 = new int[n2][m2];
// INPUT: 2nd
for(int rows = 0; rows <= n2-1; rows++)
{
for(int cols = 0; cols <= m2-1; cols++)
{
arr2[rows][cols] = scn.nextInt();
}
}
if(m1 == n2)
{
int[][] res = new int[n1][m2];
for(int row=0; row<n1; row++)
{
for(int col=0; col<m2; col++)
{
// C[row][col]
// First matrix -> row, Second matrix -> col
int ans = 0;
for(int k = 0; k < m1; k++) // or for(int k=0; k<n2; k++)
{
ans = ans +
arr1[row][k] * arr2[k][col];
}
res[row][col] = ans;
System.out.print(ans + " ");
}
System.out.println();
}
}
else
{
// Matrix Multiplication not possible
System.out.println("Invalid input");
}
}
}