-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP32_MatrixAdditionAndMultiplication.java
More file actions
64 lines (55 loc) · 1.91 KB
/
Copy pathP32_MatrixAdditionAndMultiplication.java
File metadata and controls
64 lines (55 loc) · 1.91 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
56
57
58
59
60
61
62
63
64
package programs;
/**
* ============================================================
* PROGRAM 32: Matrix Addition and Matrix Multiplication
* ============================================================
* Problem: WAP to perform:
* a) Matrix Addition: C[i][j] = A[i][j] + B[i][j]
* b) Matrix Multiplication: C[i][j] = Σ (A[i][k] * B[k][j])
* ============================================================
*/
public class P32_MatrixAdditionAndMultiplication {
public static int[][] addMatrices(int[][] a, int[][] b) {
int rows = a.length, cols = a[0].length;
int[][] res = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) res[i][j] = a[i][j] + b[i][j];
}
return res;
}
public static int[][] multiplyMatrices(int[][] a, int[][] b) {
int r1 = a.length, c1 = a[0].length;
int r2 = b.length, c2 = b[0].length;
if (c1 != r2) throw new IllegalArgumentException("Matrix dimensions invalid for multiplication!");
int[][] res = new int[r1][c2];
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
res[i][j] += a[i][k] * b[k][j];
}
}
}
return res;
}
public static void printMatrix(int[][] m) {
for (int[] row : m) {
System.out.print(" [ ");
for (int val : row) System.out.printf("%4d ", val);
System.out.println("]");
}
}
public static void main(String[] args) {
int[][] m1 = {
{1, 2, 3},
{4, 5, 6}
};
int[][] m2 = {
{7, 8},
{9, 1},
{2, 3}
};
System.out.println("=== MATRIX MULTIPLICATION (2x3 * 3x2 -> 2x2) ===");
int[][] mult = multiplyMatrices(m1, m2);
printMatrix(mult);
}
}