-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMulti.java
More file actions
40 lines (34 loc) · 1.16 KB
/
MatrixMulti.java
File metadata and controls
40 lines (34 loc) · 1.16 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
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class MatrixMulti {
public static void main(String[] args) {
Random random = new Random();
long startTime = System.nanoTime();
long endTime = System.nanoTime();
long timeElapsed = endTime - startTime;
Scanner scanner = new Scanner(System.in);
int n = scanner.nextint();
int[][] Arr1 = new int[n][n];
int[][] Arr2 = new int[n][n];
int[][] Ans = new int[n][n];
int joy = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Arr1[i][j] = random.nextInt(10);
Arr2[i][j] = random.nextInt(10);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
joy = 0;
for (int k = 0; k < n; k++) {
joy += Arr1[i][k] * Arr2[k][j];
}
Ans[i][j] = joy;
System.out.print(Ans[i][j] + " ");
}
}
System.out.println(" ");
System.out.println("Elapsed time in nanoseconds: "+timeElapsed);
}
}