-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixMultiplication.java
70 lines (61 loc) · 1.88 KB
/
MatrixMultiplication.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
67
68
69
70
/* Computer Programming 1
*/
//Matrix Multiplication Example
import java.io.*;
import java.util.*;
public class MatrixMultiplication
{
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Please Enter array size");
int size = reader.nextInt();
int [][] arr1 = new int [size][size];
int [][] arr2 = new int [size][size];
int [][] arr3 = new int [size][size];
int cellValue=0,j,i,k;
//Creates Random instance to generate random values
Random rand = new Random();
//filling the arrays with some values
for(i =0; i<size; i++){
for(j = 0; j<size;j++){
// Generates random values between 1 and 9
arr1 [i][j] = 1+rand.nextInt(9);
arr2 [i][j] = 1+rand.nextInt(9);
}
}
//Doing the multiplication
for(i = 0; i<size; i++){//loop1
for(j =0;j<size;j++){//loop2
for(k=0;k<size;k++){//loop3
cellValue += arr1[i][k] * arr2[k][j];
}//end loop1
arr3[i][j] = cellValue;
cellValue =0;
}//end loop2
}//end loop3
//printing arr1
System.out.print("\n array1\n");
for(i =0; i<size; i++){
for(j = 0; j<size;j++){
System.out.print(arr1 [i][j]+"\t");
}
System.out.println();
}
//printing arr2
System.out.print("\n array2\n");
for(i =0; i<size; i++){
for(j = 0; j<size;j++){
System.out.print(arr2 [i][j]+"\t");
}
System.out.println();
}
//printing arr3
System.out.print("\n array3\n");
for(i =0; i<size; i++){
for(j = 0; j<size;j++){
System.out.print(arr3 [i][j]+"\t");
}
System.out.println();
}
}
}