-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixCalculator.cpp
368 lines (334 loc) · 9.58 KB
/
MatrixCalculator.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <iostream>
#include <conio.h>
using namespace std;
// matrix A
int A[3][3] = {{2, 3, 6},
{3, 4, 5},
{6, 5, 9}};
// Matrix B
int B[3][3] = {{1, 0, 0},
{0, 0, 0},
{0, 0, -4}};
// Matrix C
int C[3][3];
void InputValueInMatrixA(); // function for taking input values in Matrix A and B
void InputValueInMatrixB();
void print(int D[3][3]); // function for displaying matrix values
void sum(int A[3][3], int B[3][3]); // function for adding two matrix
void subtraction(int A[3][3], int B[3][3]); // function for subtraction of two matrix
void multiplication(int A[3][3], int B[3][3]); // function for multiplication of two matrix
void scalarMultiplication(int A[3][3]); // function for sacalar multiplication with matrix
bool isIdentityMatrix(int M[3][3]); // function for checking property of identity matrix
void transposeMatrix(int M[3][3]); // function for transposing matrix
bool isDiagonalMatrix(int M[3][3]); // function for checking property of diagonal matrix
bool isSymmetric(int M[3][3]); // function for checking is matrix is symmetric
char menu()
{
cout << "******************************************" << endl;
cout << "* MATRIX CALCULATOR *" << endl;
cout << "******************************************" << endl;
cout << endl;
cout << " 1. Sum " << endl;
cout << " 2. Subtraction " << endl;
cout << " 3. Multiplication " << endl;
cout << " 4. Scalar Multiplication " << endl;
cout << " 5. Transpose " << endl;
cout << " 6. Check Properties " << endl;
cout << " 7. Exit " << endl;
char op;
cin >> op;
return op;
}
main()
{
while (true)
{
system("cls");
char op;
op = menu();
if (op == '1') // sum
{
InputValueInMatrixA();
InputValueInMatrixB();
print(A);
print(B);
sum(A, B);
print(C);
}
else if (op == '2')
{
InputValueInMatrixA();
InputValueInMatrixB();
print(A);
print(B);
subtraction(A, B);
print(C);
}
else if (op == '3')
{
InputValueInMatrixA();
InputValueInMatrixB();
print(A);
print(B);
multiplication(A, B);
print(C);
}
else if (op == '4')
{
InputValueInMatrixA();
print(A);
scalarMultiplication(A);
print(C);
}
else if (op == '5')
{
InputValueInMatrixA();
print(A);
transposeMatrix(A);
cout << " Transpose Matrix" << endl;
print(C);
}
else if (op == '6')
{
InputValueInMatrixA();
print(A);
if (isIdentityMatrix(A))
{
cout << " Identity Matrix " << endl;
}
else
{
cout << " Not Identity Matrix " << endl;
}
if (isDiagonalMatrix(A))
{
cout << " Diagonal Matrix " << endl;
}
else
{
cout << " Not Diagonal Matrix " << endl;
}
if (isSymmetric(A))
{
cout << " Symmetric Matrix " << endl;
}
else
{
cout << " Not Symmetric Matrix " << endl;
}
}
else if (op == '7')
{
break;
}
cout << " Press Any key to continue....";
getch();
}
// InputValueInMatrixA();
// InputValueInMatrixB();
// print(A);
// print(B);
// sum(A, B);
// print(C);
// subtraction(A, B);
// print(C);
// multiplication(A, B);
// print(C);
// scalarMultiplication(A);
// print(C);
// if (isIdentityMatrix(A))
// {
// cout << " Identity Matrix " << endl;
// }
// else
// {
// cout << " Not Identity Matrix " << endl;
// }
// transposeMatrix(A);
// cout << " Transpose Matrix" << endl;
// print(C);
// if (isDiagonalMatrix(A))
// {
// cout << " Diagonal Matrix " << endl;
// }
// else
// {
// cout << " Not Diagonal Matrix " << endl;
// }
// if (isSymmetric(A))
// {
// cout << " Symmetric Matrix " << endl;
// }
// else
// {
// cout << " Not Symmetric Matrix " << endl;
// }
}
// function for displaying matrix values
void print(int D[3][3])
{
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
cout << "\t" << D[x][y];
}
cout << endl;
}
cout << endl;
}
// function for taking input values in Matrix A and B
void InputValueInMatrixA()
{
cout << " Enter the values of Matrix A " << endl;
for (int x = 0; x < 3; x++) // taking values in matrix A
{
for (int y = 0; y < 3; y++)
{
cout << " For R" << x + 1 << "C" << y + 1 << " : ";
cin >> A[x][y];
cout << endl;
}
}
}
void InputValueInMatrixB()
{
cout << " Enter the values of Matrix B " << endl;
for (int x = 0; x < 3; x++) // taking values in matrix A
{
for (int y = 0; y < 3; y++)
{
cout << " For R" << x + 1 << "C" << y + 1 << " : ";
cin >> B[x][y];
cout << endl;
}
}
}
// function for adding two matrix
void sum(int A[3][3], int B[3][3])
{
cout << " Addition of Matrices is : " << endl;
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
C[x][y] = A[x][y] + B[x][y]; // adding values of matrix A and B
}
}
}
// function for subtraction of two matrix
void subtraction(int A[3][3], int B[3][3])
{
cout << " Subtraction of Matrices is : " << endl;
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
C[x][y] = A[x][y] - B[x][y]; // subtracting values of matrix A and B
}
}
}
// function for multiplication of two matrix
void multiplication(int A[3][3], int B[3][3])
{
cout << " Multiplication of Matrices is : " << endl;
// multiplying rows wiyh coloumns
C[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0] + A[0][2] * B[2][0];
C[0][1] = A[0][0] * B[0][1] + A[0][1] * B[1][1] + A[0][2] * B[2][1];
C[0][2] = A[0][0] * B[0][2] + A[0][1] * B[1][2] + A[0][2] * B[2][2];
C[1][0] = A[1][0] * B[0][0] + A[1][1] * B[1][0] + A[1][2] * B[2][0];
C[1][1] = A[1][0] * B[0][1] + A[1][1] * B[1][1] + A[1][2] * B[2][1];
C[1][2] = A[1][0] * B[0][2] + A[1][1] * B[1][2] + A[1][2] * B[2][2];
C[2][0] = A[2][0] * B[0][0] + A[2][1] * B[1][0] + A[2][2] * B[2][0];
C[2][1] = A[2][0] * B[0][1] + A[2][1] * B[1][1] + A[2][2] * B[2][1];
C[2][2] = A[2][0] * B[0][2] + A[2][1] * B[1][2] + A[2][2] * B[2][2];
}
// function for sacalar multiplication with matrix
void scalarMultiplication(int A[3][3])
{
int num;
cout << " Enter the you want to multiply with ... : ";
cin >> num;
cout << " Scalar Multiplication of Matrix with " << num << " : " << endl;
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
C[x][y] = A[x][y] * num; // multiplying matrix with scalar value
}
}
}
// function for checking property of identity matrix
bool isIdentityMatrix(int M[3][3])
{
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
if (x == y)
{
if (M[x][y] != 1) // if the diagonal values are not equal to 1 than return false
{
return false;
}
}
else
{
if (M[x][y] != 0) // if the other values are not equal to 0 than return false
{
return false;
}
}
}
}
return true;
}
// function for transposing matrix
void transposeMatrix(int M[3][3])
{
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
C[y][x] = A[x][y]; // giving the values of rows to columns
}
}
}
// function for checking property of diagonal matrix
bool isDiagonalMatrix(int M[3][3])
{
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
if (x == y) // skiping the diagonal values
{
continue;
}
else
{
if (M[x][y] != 0) // if the other values are not equal to 0 than return false
{
return false;
}
}
}
}
return true;
}
// function for checking is matrix is symmetric
bool isSymmetric(int M[3][3])
{
transposeMatrix(M); // changing rows into coloumns and saving in matrix C
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
if (C[x][y] != M[x][y]) // if Matrix C and Matrix M (which is passed from main) are not same at any point return false
{
return false;
}
}
}
return true;
}