Skip to content

Commit 0dfe7ab

Browse files
Matrix Calculator
The matrix calculator is developed using a fixed 2d array of size 3x3. It contains the following options: 1. Sum 2. Subtraction 3. Multiplication 4. Scalar Multiplication 5. Transpose 6. Check Properties 7. Exit
0 parents  commit 0dfe7ab

File tree

2 files changed

+368
-0
lines changed

2 files changed

+368
-0
lines changed

MatrixCalculator.cpp

+368
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,368 @@
1+
#include <iostream>
2+
#include <conio.h>
3+
4+
using namespace std;
5+
6+
// matrix A
7+
int A[3][3] = {{2, 3, 6},
8+
{3, 4, 5},
9+
{6, 5, 9}};
10+
11+
// Matrix B
12+
int B[3][3] = {{1, 0, 0},
13+
{0, 0, 0},
14+
{0, 0, -4}};
15+
16+
// Matrix C
17+
int C[3][3];
18+
19+
void InputValueInMatrixA(); // function for taking input values in Matrix A and B
20+
void InputValueInMatrixB();
21+
void print(int D[3][3]); // function for displaying matrix values
22+
void sum(int A[3][3], int B[3][3]); // function for adding two matrix
23+
void subtraction(int A[3][3], int B[3][3]); // function for subtraction of two matrix
24+
void multiplication(int A[3][3], int B[3][3]); // function for multiplication of two matrix
25+
void scalarMultiplication(int A[3][3]); // function for sacalar multiplication with matrix
26+
bool isIdentityMatrix(int M[3][3]); // function for checking property of identity matrix
27+
void transposeMatrix(int M[3][3]); // function for transposing matrix
28+
bool isDiagonalMatrix(int M[3][3]); // function for checking property of diagonal matrix
29+
bool isSymmetric(int M[3][3]); // function for checking is matrix is symmetric
30+
31+
char menu()
32+
{
33+
cout << "******************************************" << endl;
34+
cout << "* MATRIX CALCULATOR *" << endl;
35+
cout << "******************************************" << endl;
36+
cout << endl;
37+
cout << " 1. Sum " << endl;
38+
cout << " 2. Subtraction " << endl;
39+
cout << " 3. Multiplication " << endl;
40+
cout << " 4. Scalar Multiplication " << endl;
41+
cout << " 5. Transpose " << endl;
42+
cout << " 6. Check Properties " << endl;
43+
cout << " 7. Exit " << endl;
44+
char op;
45+
cin >> op;
46+
return op;
47+
}
48+
49+
main()
50+
{
51+
while (true)
52+
{
53+
system("cls");
54+
55+
char op;
56+
op = menu();
57+
58+
if (op == '1') // sum
59+
{
60+
InputValueInMatrixA();
61+
InputValueInMatrixB();
62+
print(A);
63+
print(B);
64+
sum(A, B);
65+
print(C);
66+
}
67+
else if (op == '2')
68+
{
69+
InputValueInMatrixA();
70+
InputValueInMatrixB();
71+
print(A);
72+
print(B);
73+
subtraction(A, B);
74+
print(C);
75+
}
76+
else if (op == '3')
77+
{
78+
InputValueInMatrixA();
79+
InputValueInMatrixB();
80+
print(A);
81+
print(B);
82+
multiplication(A, B);
83+
print(C);
84+
}
85+
else if (op == '4')
86+
{
87+
InputValueInMatrixA();
88+
print(A);
89+
scalarMultiplication(A);
90+
print(C);
91+
}
92+
else if (op == '5')
93+
{
94+
InputValueInMatrixA();
95+
print(A);
96+
transposeMatrix(A);
97+
cout << " Transpose Matrix" << endl;
98+
print(C);
99+
}
100+
else if (op == '6')
101+
{
102+
InputValueInMatrixA();
103+
print(A);
104+
if (isIdentityMatrix(A))
105+
{
106+
cout << " Identity Matrix " << endl;
107+
}
108+
else
109+
{
110+
cout << " Not Identity Matrix " << endl;
111+
}
112+
if (isDiagonalMatrix(A))
113+
{
114+
cout << " Diagonal Matrix " << endl;
115+
}
116+
else
117+
{
118+
cout << " Not Diagonal Matrix " << endl;
119+
}
120+
121+
if (isSymmetric(A))
122+
{
123+
cout << " Symmetric Matrix " << endl;
124+
}
125+
else
126+
{
127+
cout << " Not Symmetric Matrix " << endl;
128+
}
129+
}
130+
else if (op == '7')
131+
{
132+
break;
133+
}
134+
cout << " Press Any key to continue....";
135+
getch();
136+
}
137+
138+
// InputValueInMatrixA();
139+
// InputValueInMatrixB();
140+
// print(A);
141+
// print(B);
142+
// sum(A, B);
143+
// print(C);
144+
// subtraction(A, B);
145+
// print(C);
146+
// multiplication(A, B);
147+
// print(C);
148+
// scalarMultiplication(A);
149+
// print(C);
150+
151+
// if (isIdentityMatrix(A))
152+
// {
153+
// cout << " Identity Matrix " << endl;
154+
// }
155+
// else
156+
// {
157+
// cout << " Not Identity Matrix " << endl;
158+
// }
159+
160+
// transposeMatrix(A);
161+
// cout << " Transpose Matrix" << endl;
162+
// print(C);
163+
164+
// if (isDiagonalMatrix(A))
165+
// {
166+
// cout << " Diagonal Matrix " << endl;
167+
// }
168+
// else
169+
// {
170+
// cout << " Not Diagonal Matrix " << endl;
171+
// }
172+
173+
// if (isSymmetric(A))
174+
// {
175+
// cout << " Symmetric Matrix " << endl;
176+
// }
177+
// else
178+
// {
179+
// cout << " Not Symmetric Matrix " << endl;
180+
// }
181+
}
182+
183+
// function for displaying matrix values
184+
void print(int D[3][3])
185+
{
186+
for (int x = 0; x < 3; x++)
187+
{
188+
for (int y = 0; y < 3; y++)
189+
{
190+
cout << "\t" << D[x][y];
191+
}
192+
cout << endl;
193+
}
194+
cout << endl;
195+
}
196+
197+
// function for taking input values in Matrix A and B
198+
void InputValueInMatrixA()
199+
{
200+
cout << " Enter the values of Matrix A " << endl;
201+
for (int x = 0; x < 3; x++) // taking values in matrix A
202+
{
203+
for (int y = 0; y < 3; y++)
204+
{
205+
cout << " For R" << x + 1 << "C" << y + 1 << " : ";
206+
cin >> A[x][y];
207+
cout << endl;
208+
}
209+
}
210+
}
211+
212+
void InputValueInMatrixB()
213+
{
214+
cout << " Enter the values of Matrix B " << endl;
215+
for (int x = 0; x < 3; x++) // taking values in matrix A
216+
{
217+
for (int y = 0; y < 3; y++)
218+
{
219+
cout << " For R" << x + 1 << "C" << y + 1 << " : ";
220+
cin >> B[x][y];
221+
cout << endl;
222+
}
223+
}
224+
}
225+
226+
// function for adding two matrix
227+
void sum(int A[3][3], int B[3][3])
228+
{
229+
cout << " Addition of Matrices is : " << endl;
230+
for (int x = 0; x < 3; x++)
231+
{
232+
for (int y = 0; y < 3; y++)
233+
{
234+
C[x][y] = A[x][y] + B[x][y]; // adding values of matrix A and B
235+
}
236+
}
237+
}
238+
239+
// function for subtraction of two matrix
240+
void subtraction(int A[3][3], int B[3][3])
241+
{
242+
cout << " Subtraction of Matrices is : " << endl;
243+
for (int x = 0; x < 3; x++)
244+
{
245+
for (int y = 0; y < 3; y++)
246+
{
247+
C[x][y] = A[x][y] - B[x][y]; // subtracting values of matrix A and B
248+
}
249+
}
250+
}
251+
252+
// function for multiplication of two matrix
253+
void multiplication(int A[3][3], int B[3][3])
254+
{
255+
cout << " Multiplication of Matrices is : " << endl;
256+
257+
// multiplying rows wiyh coloumns
258+
259+
C[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0] + A[0][2] * B[2][0];
260+
C[0][1] = A[0][0] * B[0][1] + A[0][1] * B[1][1] + A[0][2] * B[2][1];
261+
C[0][2] = A[0][0] * B[0][2] + A[0][1] * B[1][2] + A[0][2] * B[2][2];
262+
263+
C[1][0] = A[1][0] * B[0][0] + A[1][1] * B[1][0] + A[1][2] * B[2][0];
264+
C[1][1] = A[1][0] * B[0][1] + A[1][1] * B[1][1] + A[1][2] * B[2][1];
265+
C[1][2] = A[1][0] * B[0][2] + A[1][1] * B[1][2] + A[1][2] * B[2][2];
266+
267+
C[2][0] = A[2][0] * B[0][0] + A[2][1] * B[1][0] + A[2][2] * B[2][0];
268+
C[2][1] = A[2][0] * B[0][1] + A[2][1] * B[1][1] + A[2][2] * B[2][1];
269+
C[2][2] = A[2][0] * B[0][2] + A[2][1] * B[1][2] + A[2][2] * B[2][2];
270+
}
271+
272+
// function for sacalar multiplication with matrix
273+
void scalarMultiplication(int A[3][3])
274+
{
275+
int num;
276+
277+
cout << " Enter the you want to multiply with ... : ";
278+
cin >> num;
279+
cout << " Scalar Multiplication of Matrix with " << num << " : " << endl;
280+
281+
for (int x = 0; x < 3; x++)
282+
{
283+
for (int y = 0; y < 3; y++)
284+
{
285+
C[x][y] = A[x][y] * num; // multiplying matrix with scalar value
286+
}
287+
}
288+
}
289+
290+
// function for checking property of identity matrix
291+
bool isIdentityMatrix(int M[3][3])
292+
{
293+
for (int x = 0; x < 3; x++)
294+
{
295+
for (int y = 0; y < 3; y++)
296+
{
297+
if (x == y)
298+
{
299+
if (M[x][y] != 1) // if the diagonal values are not equal to 1 than return false
300+
{
301+
return false;
302+
}
303+
}
304+
else
305+
{
306+
if (M[x][y] != 0) // if the other values are not equal to 0 than return false
307+
{
308+
return false;
309+
}
310+
}
311+
}
312+
}
313+
return true;
314+
}
315+
316+
// function for transposing matrix
317+
void transposeMatrix(int M[3][3])
318+
{
319+
320+
for (int x = 0; x < 3; x++)
321+
{
322+
for (int y = 0; y < 3; y++)
323+
{
324+
C[y][x] = A[x][y]; // giving the values of rows to columns
325+
}
326+
}
327+
}
328+
329+
// function for checking property of diagonal matrix
330+
bool isDiagonalMatrix(int M[3][3])
331+
{
332+
for (int x = 0; x < 3; x++)
333+
{
334+
for (int y = 0; y < 3; y++)
335+
{
336+
if (x == y) // skiping the diagonal values
337+
{
338+
continue;
339+
}
340+
else
341+
{
342+
if (M[x][y] != 0) // if the other values are not equal to 0 than return false
343+
{
344+
return false;
345+
}
346+
}
347+
}
348+
}
349+
return true;
350+
}
351+
352+
// function for checking is matrix is symmetric
353+
bool isSymmetric(int M[3][3])
354+
{
355+
transposeMatrix(M); // changing rows into coloumns and saving in matrix C
356+
357+
for (int x = 0; x < 3; x++)
358+
{
359+
for (int y = 0; y < 3; y++)
360+
{
361+
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
362+
{
363+
return false;
364+
}
365+
}
366+
}
367+
return true;
368+
}

MatrixCalculator.exe

51.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)