-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathMatrix Calculator
272 lines (266 loc) · 5.38 KB
/
Matrix Calculator
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
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<cctype>
using namespace std;
int determinant(int odr,int a[10][10])
{
int det=0,d1,d2,d3;
if(odr==2)
return (a[0][0]*a[1][1]-a[0][1]*a[1][0]);
else
{
d1=a[0][0]*(a[1][1]*a[2][2]-a[2][1]*a[1][2]);
d2=a[0][1]*(a[1][0]*a[2][2]-a[2][0]*a[1][2]);
d3=a[0][2]*(a[1][0]*a[2][1]-a[2][0]*a[1][1]);
det=d1-d2+d3;
}
return det;
}
int rank(int odr,int a[10][10])
{
int det=0,d1,d2,d3;
if(odr==2)
{
det=(a[0][0]*a[1][1]-a[0][1]*a[1][0]);
if(det==0)
return 1;
else
return 2;
}
else
{
d1=a[0][0]*(a[1][1]*a[2][2]-a[2][1]*a[1][2]);
d2=a[0][1]*(a[1][0]*a[2][2]-a[2][0]*a[1][2]);
d3=a[0][2]*(a[1][0]*a[2][1]-a[2][0]*a[1][1]);
det=d1-d2+d3;
if(det!=0)
return 3;
else
return 2;
}
}
main()
{
int n,row,col,odr,a[10][10],b[10][10],c[10][10],adj[5][5];
char d;
//Welcome Screen
do
{
cout<<"\t\t\t\t\tSelect one of the following\n"<<"\t\t\t\t\t***************************\n";
cout<<"1. Addition of two matrices\n";
cout<<"2. Subtraction of two matrices\n";
cout<<"3. Multiplication of two matrices\n";
cout<<"4. Determinant of Matrix\n";
cout<<"5. Adjoint of Matrix\n";
cout<<"6. Inverse of Matrix\n";
cout<<"7. Rank of Matrix\n";
cout<<"8. Quit\n";
cout<<"\n********************************************\n\n";
//taking choice input
cin>>n;
if(n==1 || n==2 || n==3)
{
//matrix elements
cout<<"Enter the number of rows:\n";
cin>>row;
cout<<"Enter the number of columns:\n";
cin>>col;
cout<<"Enter the elements of first matrix(row wise)\n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter the elements of second matrix(row wise)\n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cin>>b[i][j];
}
}
}
if(n==4 || n==5 || n==6)
{
cout<<"Enter the Order of Matrix(Maximum Order=3)\n";
cin>>odr;
cout<<"Enter the elements of the matrix(row wise)\n";
for(int i=0;i<odr;i++)
{
for(int j=0;j<odr;j++)
{
cin>>a[i][j];
}
}
}
if(n==7)
{
cout<<"Enter Order of Square Matrix(Maximum Order=3)\n";
cin>>odr;
cout<<"Enter the elements of the matrix(row wise)\n";
for(int i=0;i<odr;i++)
{
for(int j=0;j<odr;j++)
{
cin>>a[i][j];
}
}
}
switch(n)
{
case 1:
cout<<"Sum of the Matrices equals to\n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<endl;
}
break;
case 2:
cout<<"Subtraction equals to\n";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
c[i][j]=a[i][j]-b[i][j];
}
}
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<endl;
}
break;
case 3:
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
c[i][j]=0;
for(int k=0;k<col;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<endl;
}
break;
case 4:
cout<<"Determinant of Matrix equals to:\n"<<determinant(odr,a)<<endl;
break;
case 5:
if(odr==2)
{
adj[0][0]=pow(-1,2)*a[1][1];
adj[0][1]=pow(-1,1)*a[0][1];
adj[1][0]=pow(-1,1)*a[1][0];
adj[1][1]=pow(-1,2)*a[0][0];
cout<<"Adjoint of Matrix equals to:\n";
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout<<adj[i][j]<<"\t";
}
cout<<endl;
}
}
if(odr==3)
{
adj[0][0]=(a[1][1]*a[2][2])-(a[2][1]*a[1][2]);
adj[0][1]=(a[1][2]*a[2][0])-(a[2][2]*a[1][0]);
adj[0][2]=(a[1][0]*a[2][1])-(a[2][0]*a[1][1]);
adj[1][0]=(a[2][1]*a[0][2])-(a[0][1]*a[2][2]);
adj[1][1]=(a[0][0]*a[2][2])-(a[0][2]*a[2][0]);
adj[1][2]=(a[2][0]*a[0][1])-(a[0][0]*a[2][1]);
adj[2][0]=(a[0][1]*a[1][2])-(a[1][1]*a[0][2]);
adj[2][1]=(a[1][0]*a[0][2])-(a[1][2]*a[0][0]);
adj[2][2]=(a[1][1]*a[0][0])-(a[0][1]*a[1][0]);
cout<<"Adjoint of Matrix equals to:\n";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<adj[i][j]<<"\t";
}
cout<<endl;
}
}
break;
case 6:
if(odr==2)
{
adj[0][0]=pow(-1,2)*a[1][1];
adj[0][1]=pow(-1,1)*a[0][1];
adj[1][0]=pow(-1,1)*a[1][0];
adj[1][1]=pow(-1,2)*a[0][0];
cout<<"Inverse of Matrix equals to:\n";
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout<<adj[i][j]<<"/"<<determinant(odr,a)<<"\t";
}
cout<<endl;
}
}
if(odr==3)
{
adj[0][0]=(a[1][1]*a[2][2])-(a[2][1]*a[1][2]);
adj[0][1]=(a[1][2]*a[2][0])-(a[2][2]*a[1][0]);
adj[0][2]=(a[1][0]*a[2][1])-(a[2][0]*a[1][1]);
adj[1][0]=(a[2][1]*a[0][2])-(a[0][1]*a[2][2]);
adj[1][1]=(a[0][0]*a[2][2])-(a[0][2]*a[2][0]);
adj[1][2]=(a[2][0]*a[0][1])-(a[0][0]*a[2][1]);
adj[2][0]=(a[0][1]*a[1][2])-(a[1][1]*a[0][2]);
adj[2][1]=(a[1][0]*a[0][2])-(a[1][2]*a[0][0]);
adj[2][2]=(a[1][1]*a[0][0])-(a[0][1]*a[1][0]);
cout<<"Inverse of Matrix equals to:\n";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<adj[i][j]<<"/"<<determinant(odr,a)<<"\t";
}
cout<<endl;
}
}
break;
case 7:
cout<<"Rank of Matrix equals to:\n"<<rank(odr,a)<<endl;
break;
case 8:
exit(0);
break;
}
cout<<"********************************************";
cout<<"\n\nDo you want to continue? (Y/N)\n";
cin>>d;
d=toupper(d);
}while(d=='Y');
return 0;
}