forked from Shreyasheeetal20/HACKTOBERFEST22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixOperation.java
202 lines (191 loc) · 6.51 KB
/
MatrixOperation.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package college_programs;
import java.util.*;
public class MatrixOperation
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter row numbers or column numbers : ");
int index = sc.nextInt();
int[][] matrix1 = new int[index][index];
int[][] matrix2 = new int[index][index];
operation op = new operation();
matrix1 = op.create(index);
op.display(matrix1,index);
matrix2 = op.create(index);
op.display(matrix2,index);
while(true)
{
System.out.println("Enter your choice : ");
System.out.println("1)Addition"+"\n"+"2)Subtraction"+"\n"+"3)Multiplication"+"\n"+"4)Division"+"\n"+"5)Scalar multiplication for matrix 1"+"\n"+"6)Scalar multiplication for matrix 2"+"\n"+"7)Transpose of 1st matrix"+"\n"+"8)Transpose of 2nd matrix");
int choice = sc.nextInt();
switch (choice)
{
case 1 -> {
int[][] result1 = op.addition(matrix1, matrix2, index);
op.display(result1,index);
}
case 2 -> {
int[][] result2 = op.subtraction(matrix1, matrix2, index);
op.display(result2,index);
}
case 3 -> {
int[][] result3 = op.multiplication(matrix1, matrix2, index);
op.display(result3,index);
}
case 4 -> {
int[][] result4 = op.division(matrix1, matrix2, index);
op.display(result4,index);
}
case 5 -> {
int[][] result5 = op.scalar_multi1(matrix1, matrix2, index);
System.out.println("Scalar multiplication of first matrix is :");
op.display(result5,index);
}
case 6 -> {
int[][] result6 = op.scalar_multi2(matrix1, matrix2, index);
System.out.println("Scalar multiplication of Second matrix is :");
op.display(result6,index);
}
case 7 -> {
int[][] result7 = op.transpose1(matrix1, matrix2, index);
System.out.println("Transpose of first matrix is : ");
op.display(result7,index);
}
case 8 -> {
int[][] result8 = op.transpose2(matrix1, matrix2, index);
System.out.println("Transpose of second matrix is : ");
op.display(result8,index);
}
default -> System.out.println("Enter the valid choice...!!!!");
}
}
}
}
class operation
{
public int[][] create(int index)
{
int n = (int)index;
int[][] matrix = new int[n][n];
Scanner sc = new Scanner(System.in);
System.out.printf("Enter the matrix(%d * %d) : ",n,n);
for(int i = 0;i < n;i++)
{
for(int j = 0;j < n;j++ )
{
matrix[i][j] = sc.nextInt();
}
}
return matrix;
}
public void display(int[][] matrix,int index)
{
int n = (int)index;
System.out.println("matrix is : ");
for(int i = 0;i < n;i++)
{
for(int j = 0;j < n;j++ )
{
System.out.print(" "+matrix[i][j]);
}
System.out.println();
}
}
public int[][] addition(int[][] matrix1,int[][] matrix2,int index)
{
int n = (int)index;
int[][] matrix3 = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix3[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
return matrix3;
}
public int[][] subtraction(int[][] matrix1,int[][] matrix2,int index)
{
int n = (int)index;
int[][] matrix3 = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix3[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
return matrix3;
}
public int[][] multiplication(int[][] matrix1,int[][] matrix2,int index)
{
int n = (int)index;
int[][] matrix3 = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
matrix3[i][j] = matrix3[i][j] + matrix1[i][k] * matrix2[k][j];
}
}
}
return matrix3;
}
public int[][] division(int[][] matrix1,int[][] matrix2,int index)
{
int n = (int)index;
int[][] matrix3 = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix3[i][j] = matrix1[i][j] / matrix2[i][j];
}
}
return matrix3;
}
public int[][] scalar_multi1(int[][] matrix1,int[][] matrix2,int index)
{
Scanner sc = new Scanner(System.in);
int n = (int)index;
int[][] matrix3 = new int[n][n];
System.out.println("Enter the no. to multiply the matrix : ");
int no = sc.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix3[i][j] = no * matrix1[i][j];
}
}
return matrix3;
}
public int[][] scalar_multi2(int[][] matrix1,int[][] matrix2,int index)
{
Scanner sc = new Scanner(System.in);
int n = (int)index;
int[][] matrix3 = new int[n][n];
System.out.println("Enter the no. to multiply the matrix : ");
int no = sc.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix3[i][j] = no * matrix2[i][j];
}
}
return matrix3;
}
public int[][] transpose1(int[][] matrix1,int[][] matrix2,int index)
{
int n = (int)index;
int[][] matrix3 = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix3[i][j]=matrix1[j][i];
}
}
return matrix3;
}
public int[][] transpose2(int[][] matrix1,int[][] matrix2,int index)
{
int n = (int)index;
int[][] matrix3 = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix3[i][j]=matrix2[j][i];
}
}
return matrix3;
}
}