-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixmultiply.java
More file actions
43 lines (42 loc) · 1.36 KB
/
matrixmultiply.java
File metadata and controls
43 lines (42 loc) · 1.36 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
import java.util.Scanner;
public class matrixmultiply {
public static void main(String[] args) {
int m, n, o, p;
Scanner sc = new Scanner(System.in);
m = sc.nextInt();
n = sc.nextInt();
int arr1[][] = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr1[i][j] = sc.nextInt();
}
}
o = sc.nextInt();
p = sc.nextInt();
if (o == n) {
int arr2[][] = new int[n][p];
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
arr2[i][j] = sc.nextInt();
}
}
int arr3[][] = new int[m][p];
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
for (int k = 0; k < n; k++) {
arr3[i][j] += (arr1[i][k] * arr2[k][j]);
}
}
}
System.out.println();
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
System.out.print(arr3[i][j] + " ");
}
System.out.println();
}
} else {
System.out.println("Error: the matrix cannot be multiplied");
}
}
}