-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathMatrix_multiplication.java
87 lines (61 loc) · 1.77 KB
/
Matrix_multiplication.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.Scanner;
public class MatrixMultiplication {
//method to display the result array
public static void Display(int [][] a)
{
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
//main method
public static void main(String[] args) {
//instantiating Scanner object
Scanner scn = new Scanner(System.in);
//size of 1st array
System.out.println("Enter the size of 1st array i.e row & col");
int r1 = scn.nextInt();
int c1 = scn.nextInt();
int [][] oneArray = new int[r1][c1];
//input of 1st array
System.out.println("Enter the element of 1st array");
for (int i = 0; i < oneArray.length; i++) {
for (int j = 0; j < oneArray[0].length; j++) {
oneArray[i][j] = scn.nextInt();
}
}
//size of 2nd array
System.out.println("Enter the size of 2nd array i.e row & col");
int r2 = scn.nextInt();
int c2 = scn.nextInt();
int [][] twoArray = new int[r2][c2];
//input of 2nd array
System.out.println("Enter the element of 2nd array");
for (int i = 0; i < twoArray.length; i++) {
for (int j = 0; j < twoArray[0].length; j++) {
twoArray[i][j] = scn.nextInt();
}
}
//base case
if(c1 != r2)
{
System.out.println("Invalid input");
return;
}
//new array to store the result array
int [][] prdArray = new int[r1][c2];
//multiplication of 3 arrays
for (int i = 0; i < prdArray.length; i++) {
for (int j = 0; j < prdArray[0].length; j++) {
for(int k = 0; k<c1 ; k++)
{
prdArray[i][j] = prdArray[i][j] + oneArray[i][k] * twoArray[k][j];
}
}
}
//calling display method
Display(prdArray);
}
}