-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.cpp
327 lines (305 loc) · 7.65 KB
/
matrix.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
#include <iostream>
#include <math.h>
#include <conio.h>
#include <fstream>
#include<windows.h>
using namespace std;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
int row, col;
void displayofmatrix();
void printmatrix();
void printmatrix2();
void printmatrix3();
void identifyingmatrixforsquare();
int userchoice();
void typeofmatrix();
void determinenttype();
void determinent1();
void transposeofmatrix();
void orderofmatrix();
void addtwomatrices();
int matrix[3][5]; // Dummy value for 2d array of matix
int matrix3[3][3];
int matrix2[2][2];
int sumofmatrices[2][3];
main()
{
SetConsoleTextAttribute(hConsole, 6);
cout<<"## ## ### ######## ######## #### ## ## ###### ### ## ###### ## ## ## ## ######## ####### ######## "<<endl;
cout<<"### ### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## "<<endl;
cout<<"#### #### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## "<<endl;
cout<<"## ### ## ## ## ## ######## ## ### ## ## ## ## ## ## ## ## ## ## ## ## ## ######## "<<endl;
cout<<"## ## ######### ## ## ## ## ## ## ## ######### ## ## ## ## ## ######### ## ## ## ## ## "<<endl;
cout<<"## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## "<<endl;
cout<<"## ## ## ## ## ## ## #### ## ## ###### ## ## ######## ###### ####### ######## ## ## ## ####### ## ##"<<endl;
SetConsoleTextAttribute(hConsole, 13);
int choice = userchoice();
if (choice == 1)
{
orderofmatrix();
}
if (choice == 2)
{
typeofmatrix();
}
if (choice == 3)
{
transposeofmatrix();
}
if (choice == 4)
{
determinenttype();
}
if (choice == 5)
{
addtwomatrices();
}
}
int userchoice()
{
int choice;
cout << "1. Find order of matrix." << endl;
cout << "2. Type of matrix." << endl;
cout << "3. Find transpose of matrix." << endl;
cout << "4. Find determinent." << endl;
cout << "5. Add two matrices." << endl;
cout << "4. Subtract two matrices." << endl;
cin >> choice;
return choice;
}
void displayofmatrix()
{
cout << "Enter the row size: ";
cin >> row;
cout << "Enter the column size: ";
cin >> col;
}
void printmatrix()
{
int matrix[row][col];
for (int i = 0; i < row; i++)
{
for (int f = 0; f < col; f++)
{
cout << "Enter entity: ";
cin >> matrix[i][f];
}
}
cout << "Your matrix is: " << endl;
for (int i = 0; i < row; i++)
{
for (int f = 0; f < col; f++)
{
cout << matrix[i][f] << " ";
}
cout << endl;
}
}
void printmatrix2()
{
for (int i = 0; i < 2; i++)
{
for (int f = 0; f < 2; f++)
{
cout << "Enter entity: ";
cin >> matrix2[i][f];
}
}
cout << "Your matrix is: " << endl;
for (int i = 0; i < 2; i++)
{
for (int f = 0; f < 2; f++)
{
cout << matrix2[i][f] << " ";
}
cout << endl;
}
}
void printmatrix3()
{
for (int i = 0; i < 3; i++)
{
for (int f = 0; f < 3; f++)
{
cout << "Enter entity: ";
cin >> matrix3[i][f];
}
}
cout << "Your matrix is: " << endl;
for (int i = 0; i < 3; i++)
{
for (int f = 0; f < 3; f++)
{
cout << matrix3[i][f] << " ";
}
cout << endl;
}
}
void identifyingmatrixforsquare()
{
bool is_identity = true;
bool is_diagonal = true;
// check if matrix is diagonal
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (i != j && matrix[i][j] != 0)
{
is_diagonal = false;
break;
}
}
// if (!is_diagonal)
// {
// break;
// }
}
// check if matrix is identity
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (i == j && matrix[i][j] != 1)
{
is_identity = false;
break;
}
else if (i != j && matrix[i][j] != 0)
{
is_identity = false;
break;
}
}
// if (!is_identity)
// {
// break;
// }
}
// output the result
if (is_identity)
{
cout << "The matrix is an identity matrix." << endl;
}
else if (is_diagonal)
{
cout << "The matrix is a diagonal matrix." << endl;
}
else
{
cout << "It's a Square Matrix";
}
}
void orderofmatrix()
{
cout << "The order of matrix is: " << row * col;
}
void typeofmatrix()
{
if (row == col) // identifying for diagonal,square or identity.
{
identifyingmatrixforsquare();
}
if (row != col) // identifying a rectangular matix.
{
cout << "It's a Rectangle matrix.";
}
}
void determinenttype()
{
int otype;
cout << "1. 2x2 matrix" << endl;
cout << "2. 3x3 matrix" << endl;
cin >> otype;
if (otype == 1)
{
printmatrix2();
determinent1();
}
if (otype == 2) // Pending
{
printmatrix3();
}
}
void determinent1()
{
int determinentans1, c1, c2;
c1 = matrix2[0][0] * matrix2[1][1];
c2 = matrix2[0][1] * matrix2[1][0];
determinentans1 = c1 - c2;
cout << "The Determinent of your matris is: " << determinentans1;
}
void transposeofmatrix()
{
int rows, cols;
cout << "Enter the number of rows and columns of the matrix: ";
cin >> rows >> cols;
int matrix[rows][cols];
cout << "Enter the elements of the matrix: " << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
cin >> matrix[i][j];
}
cout << "The transpose of the matrix is: " << endl;
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
cout << matrix[j][i] << " ";
cout << endl;
}
}
void addtwomatrices()
{
row = 0, col = 0;
int n_matrix[row][col];
sumofmatrices[row][col];
for (int q = 0; q < row; q++)
{
for (int t = 0; t < col; t++)
{
sumofmatrices[q][t] = 0;
matrix[q][t] = 0;
n_matrix[q][t] = 0;
}
}
cout << "Enter number of rows: " << endl;
cin >> row;
cout << "Enter number of columns: " << endl;
cin >> col;
cout << "Enter the first Matrix: " << endl;
int matrix[row][col];
for (int i = 0; i < row; i++)
{
for (int f = 0; f < col; f++)
{
cout << "Enter entity: ";
cin >> matrix[i][f];
}
}
cout << "Enter the second Matrix: " << endl;
for (int q = 0; q < row; q++)
{
for (int t = 0; t < col; t++)
{
cout << "Enter entity: ";
cin >> matrix[q][t];
}
}
for (int v = 0; v < row; v++)
{
for (int g = 0; g < col; g++)
{
sumofmatrices[v][g] = matrix[v][g] + n_matrix[v][g];
}
}
cout << "The sum of matrices is: " << endl;
for (int z = 0; z < row; z++)
{
for (int m = 0; m < col; m++)
{
cout << sumofmatrices[z][m] << "\t";
}
cout << endl;
}
}