forked from kastrahl/coding-ninjas-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixMultiply.java
More file actions
55 lines (41 loc) · 1.24 KB
/
matrixMultiply.java
File metadata and controls
55 lines (41 loc) · 1.24 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
import java.util.Scanner;
public class MatrixMultiplication{
public static void main(String[]args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[][] = new int[n][n], b[][] = new int[n][n];
a=matrix(a,n);
b=matrix(b,n);
int result[][]=multiply(a,b,n);
print(result,n);
}
public static int[][] matrix(int a[][],int n) {
int r[][]=new int[n][n];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = sc.nextInt();
}
}
return a;
}
public static int[][] multiply(int a[][],int b[][],int n){
int result[][]=new int[n][n];
for(int i=0;i<n;i++){ // row of result
for(int j=0;j<n;j++){ // column of result
for(int x=0;x<n;x++){ // column
result[i][j]+=a[i][x]*b[x][j];
}
}
}
return result;
}
public static void print(int res[][],int n){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(res[i][j]+" ");
}
System.out.println();
}
}
}