-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrices.java
127 lines (108 loc) · 4.25 KB
/
Matrices.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.util.Scanner;
public class MatrixOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of matrices: ");
int nMatrices = scanner.nextInt();
int[][][] matrices = new int[nMatrices][][];
for (int i = 0; i < nMatrices; i++) {
System.out.println("Matrix " + i + ":");
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int cols = scanner.nextInt();
matrices[i] = createMatrix(rows, cols, scanner);
}
System.out.print("Enter the operation you want to perform (addition, subtraction, multiplication): ");
String operation = scanner.next().toLowerCase();
switch (operation) {
case "addition":
performAddition(matrices);
break;
case "subtraction":
performSubtraction(matrices);
break;
case "multiplication":
performMultiplication(matrices);
break;
default:
System.out.println("Invalid operation!");
}
}
public static int[][] createMatrix(int rows, int cols, Scanner scanner) {
int[][] matrix = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Enter element (" + i + ", " + j + "): ");
matrix[i][j] = scanner.nextInt();
}
}
return matrix;
}
public static void performAddition(int[][][] matrices) {
int[][] result = matrices[0];
for (int i = 1; i < matrices.length; i++) {
if (matrices[i].length != result.length || matrices[i][0].length != result[0].length) {
System.out.println("Error");
return;
}
for (int row = 0; row < result.length; row++) {
for (int col = 0; col < result[0].length; col++) {
result[row][col] += matrices[i][row][col];
}
}
}
System.out.println("Result of addition:");
printMatrix(result);
}
public static void performSubtraction(int[][][] matrices) {
int[][] result = matrices[0];
for (int i = 1; i < matrices.length; i++) {
if (matrices[i].length != result.length || matrices[i][0].length != result[0].length) {
System.out.println("Error");
return;
}
for (int row = 0; row < result.length; row++) {
for (int col = 0; col < result[0].length; col++) {
result[row][col] -= matrices[i][row][col];
}
}
}
System.out.println("Result of subtraction:");
printMatrix(result);
}
public static void performMultiplication(int[][][] matrices) {
if (matrices.length < 2) {
System.out.println("Error");
return;
}
int[][] result = matrices[0];
for (int i = 1; i < matrices.length; i++) {
if (result[0].length != matrices[i].length || result.length == 0 || matrices[i].length == 0) {
System.out.println("Error");
return;
}
result = multiplyMatrices(result, matrices[i]);
}
if (result.length == 0) {
System.out.println("Error");
return;
}
System.out.println("Result of multiplication:");
printMatrix(result);
}
public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {
int rows1 = matrix1.length;
int cols1 = matrix1[0].length;
int rows2 = matrix2.length;
int cols2 = matrix2[0].length;
if (cols1 != rows2 || rows1 == 0 || cols1 == 0 || cols2 == 0) {
return new int[0][0];
}
int[][] result = new int[rows1][cols2];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}