-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixAddition.java
More file actions
50 lines (41 loc) · 1.55 KB
/
MatrixAddition.java
File metadata and controls
50 lines (41 loc) · 1.55 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
import java.util.Scanner;
public class MatrixAddition {
public static void printMatrix(double[][] matrix) {
for (int y = 0; y < matrix.length; y++) {
for (int x = 0; x < matrix[0].length; x++) {
System.out.print(matrix[x][y] + " ");
}
System.out.println("");
}
}
public static void main(String[] args) {
final int MATRIX_SIZE = 3;
Scanner scan = new Scanner(System.in);
double[][] matrixA = new double[MATRIX_SIZE][MATRIX_SIZE];
double[][] matrixB = new double[MATRIX_SIZE][MATRIX_SIZE];
System.out.println("Matrix A: ");
for (int y = 0; y < MATRIX_SIZE; y++) {
String num = scan.nextLine();
String[] numArray = num.split(" ", 0);
for (int x = 0; x < MATRIX_SIZE; x++) {
matrixA[x][y] = Double.parseDouble(numArray[x]);
}
}
System.out.println("Matrix B: ");
for (int y = 0; y < MATRIX_SIZE; y++) {
String num = scan.nextLine();
String[] numArray = num.split(" ", 0);
for (int x = 0; x < MATRIX_SIZE; x++) {
matrixB[x][y] = Double.parseDouble(numArray[x]);
}
}
double[][] matrixProduct = new double[MATRIX_SIZE][MATRIX_SIZE];
for (int y = 0; y < MATRIX_SIZE; y++) {
for (int x = 0; x < MATRIX_SIZE; x++) {
matrixProduct[x][y] = matrixA[x][y] + matrixB[x][y];
}
}
printMatrix(matrixProduct);
scan.close();
}
}