-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_calculator.cpp
350 lines (298 loc) · 5.81 KB
/
matrix_calculator.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
#include <iostream>
using namespace std;
class matrix
{
public:
int m, n, arr[10][10];
void readorder();
friend ostream &operator<<(ostream &, const matrix &);
friend istream &operator>>(istream &, matrix &);
matrix operator+(matrix);
matrix operator-(matrix);
matrix operator*(matrix);
matrix operator*(int scalar);
matrix operator/(int scalar);
matrix transpose();
};
void printchar(char c, int n)
{
for (; n > 0; --n)
cout << c;
}
void printjoin(char c, const char *s)
{
cout << *s;
for (++s; *s != '\0'; ++s)
cout << c << *s;
}
void printchoices(const char *choices)
{
int i;
for (i = 0; *choices != '\0'; ++choices)
{
if (!i)
cout << ++i << ". " << *choices;
else if (*choices == '.')
cout << '.' << endl
<< ++i << ". ";
else
cout << *choices;
}
cout << '.' << endl;
}
int main()
{
matrix A, B, result;
int ch, scalar;
do
{
printchar('*', 13);
cout << " ";
printjoin(' ', "MATRIX");
cout << " ";
printjoin(' ', "CALCULATOR");
cout << " ";
printchar('*', 13);
cout << endl;
printchoices("Matrix addition.Matrix subtraction.Matrix multiplication.Multiply by a scalar.Divide by a scalar.Transpose of a matrix.Exit");
cout << "Enter your choice :\t";
cin >> ch;
switch (ch)
{
case 1:
cout << "For matrix A :" << endl;
A.readorder();
cout << "For matrix B :" << endl;
B.readorder();
if (A.m != B.m || A.n != B.n)
{
cout << "Matrices must be of the same size for addition." << endl;
break;
}
cout << "For matrix A :" << endl;
cin >> A;
cout << "For matrix B :" << endl;
cin >> B;
cout << "Matrix A :" << endl;
cout << A;
cout << "Matrix B :" << endl;
cout << B;
cout << "Result of addition :" << endl;
result = A + B;
cout << result;
break;
case 2:
cout << "For matrix A :" << endl;
A.readorder();
cout << "For matrix B :" << endl;
B.readorder();
if (A.m != B.m || A.n != B.n)
{
cout << "Matrices must be of the same size for subtraction." << endl;
break;
}
cout << "For matrix A :" << endl;
cin >> A;
cout << "For matrix B :" << endl;
cin >> B;
cout << "Matrix A :" << endl;
cout << A;
cout << "Matrix B :" << endl;
cout << B;
cout << "Result of subtraction :" << endl;
result = A - B;
cout << result;
break;
case 3:
cout << "For matrix A :" << endl;
A.readorder();
cout << "For matrix B :" << endl;
B.readorder();
if (A.n != B.m)
{
cout << "The number of columns in matrix A must be equal to the number of rows in matrix B for multiplication." << endl;
break;
}
cout << "For matrix A :" << endl;
cin >> A;
cout << "For matrix B :" << endl;
cin >> B;
cout << "Matrix A :" << endl;
cout << A;
cout << "Matrix B :" << endl;
cout << B;
cout << "Result of multiplication :" << endl;
result = A * B;
cout << result;
break;
case 4:
cout << "For matrix A :" << endl;
A.readorder();
cout << "Enter the scalar :\t";
cin >> scalar;
cout << "For matrix A :" << endl;
cin >> A;
cout << "Matrix A :" << endl;
cout << A;
cout << "Result of scalar multiplication :" << endl;
result = A * scalar;
cout << result;
break;
case 5:
cout << "For matrix A :" << endl;
A.readorder();
cout << "Enter the scalar :\t";
cin >> scalar;
if (scalar == 0)
{
cout << "Cannot divide by zero." << endl;
break;
}
cout << "For matrix A :" << endl;
cin >> A;
cout << "Matrix A :" << endl;
cout << A;
cout << "Result of scalar division :" << endl;
result = A / scalar;
cout << result;
break;
case 6:
cout << "For matrix A :" << endl;
A.readorder();
cin >> A;
cout << "Matrix A :" << endl;
cout << A;
cout << "Result of transposition :" << endl;
result = A.transpose();
cout << result;
break;
case 7:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice." << endl;
}
} while (ch != 7);
return 0;
}
void matrix::readorder()
{
cout << "Enter the no. of rows :\t";
cin >> m;
cout << "Enter the no. columns :\t";
cin >> n;
}
istream &operator>>(istream &os, matrix &ptr)
{
int i, j;
cout << "Enter the items :" << endl;
for (i = 0; i < ptr.m; i++)
{
for (j = 0; j < ptr.n; j++)
{
os >> ptr.arr[i][j];
}
}
return os;
}
ostream &operator<<(ostream &ob, const matrix &pt)
{
int i, j;
for (i = 0; i < pt.m; i++)
{
for (j = 0; j < pt.n; j++)
{
ob << "\t" << pt.arr[i][j];
}
ob << endl;
}
return ob;
}
matrix matrix::operator+(matrix B)
{
int i, j;
matrix result;
result.m = m, result.n = n;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
result.arr[i][j] = arr[i][j] + B.arr[i][j];
}
}
return result;
}
matrix matrix::operator-(matrix B)
{
int i, j;
matrix result;
result.m = m, result.n = n;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
result.arr[i][j] = arr[i][j] - B.arr[i][j];
}
}
return result;
}
matrix matrix::operator*(matrix B)
{
int i, j, k;
matrix result;
result.m = m, result.n = B.n;
for (i = 0; i < m; i++)
{
for (j = 0; j < B.n; j++)
{
result.arr[i][j] = 0;
for (k = 0; k < n; k++)
{
result.arr[i][j] += arr[i][k] * B.arr[k][j];
}
}
}
return result;
}
matrix matrix::operator*(int scalar)
{
int i, j;
matrix result;
result.m = m, result.n = n;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
result.arr[i][j] = arr[i][j] * scalar;
}
}
return result;
}
matrix matrix::operator/(int scalar)
{
int i, j;
matrix result;
result.m = m, result.n = n;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
result.arr[i][j] = arr[i][j] / scalar;
}
}
return result;
}
matrix matrix::transpose()
{
int i, j;
matrix result;
result.m = n, result.n = m;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
result.arr[j][i] = arr[i][j];
}
}
return result;
}