-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrix.cpp
342 lines (252 loc) · 8.93 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <iostream>
using namespace std;
int** AllocateMatrix (int ROWS, int COLS);
int** InputMatrix ( int& ROWS, int& COLS);
void OutputMatrix (int** matrix, const int ROWS, const int COLS);
int** AddMatrix(int** matrixA, int** matrixB, const int ROWS, const int COLS);
int** TransposeMatrix(int** matrix, const int ROWS, const int COLS);
bool IsSymmetric(int** matrix, const int ROWS, const int COLS);
void InterchangeRows(int** matrix, const int ROWS, const int COLS );
void InterchangeRows( int* &row1, int* &row2 );
void deAllocateMatrix(int** &matrix, const int ROWS);
int** AllocateMatrix (int ROWS, int COLS)
{
//Code to Allocate a 2D Matrix
int** matrix = new int* [ROWS];
for ( int i = 0 ; i < ROWS ; i++)
matrix[i] = new int [COLS];
return matrix;
}
int** InputMatrix ( int& ROWS, int& COLS)
{
cout << "Total Number of Rows : ";
cin >> ROWS ;
while ( ROWS <=0 )
{
cout <<"You have entered an invalid number . Enter the Rows again : " << endl;
cin >> ROWS;
}
cout << "Total Number of Columns : ";
cin >> COLS ;
while ( COLS<=0 )
{
cout <<"You have entered an invalid number . Enter the Columns again : " << endl;
cin >> COLS;
}
int** matrix = NULL;
matrix = AllocateMatrix (ROWS , COLS);
//Code to input numbers in the 2D Matrix
cout <<endl<<"Enter the elements of the Matrix in 'row major order' : " << endl;
int** endRowPtr = matrix + ROWS;
for(int** rowPtr = matrix ; rowPtr < endRowPtr ; rowPtr++ )
{
int* endColPtr = *rowPtr + COLS;
for(int* colPtr = *rowPtr ; colPtr < endColPtr ; colPtr++)
cin >> *colPtr;
}
return matrix;
}
void OutputMatrix (int** matrix, const int ROWS, const int COLS)
{
int** endRowPtr = matrix + ROWS;
for(int** rowPtr = matrix ; rowPtr < endRowPtr ; rowPtr++ )
{
int* endColPtr = *rowPtr + COLS;
cout << endl;
for(int* colPtr = *rowPtr ; colPtr < endColPtr ; colPtr++)
cout << *colPtr << " ";
}
cout << endl;
}
int** AddMatrix(int** matrixA, int** matrixB, const int ROWS, const int COLS)
{
//Code to allocate the third Matrix i.e. Matrix C
int** matrixC = NULL;
matrixC = AllocateMatrix (ROWS , COLS);
int* colPtrA = NULL ; int* colPtrB = NULL ; int* colPtrC = NULL;
int* endColPtrA = NULL ; int* endColPtrB = NULL ; int* endColPtrC = NULL;
int** rowPtrA = matrixA;
int** rowPtrB = matrixB;
int** rowPtrC = matrixC;
int** endRowPtrA = matrixA + ROWS;
int** endRowPtrB = matrixB + ROWS;
int** endRowPtrC = matrixC + ROWS;
while( (rowPtrA < endRowPtrA) && (rowPtrB < endRowPtrB) && (rowPtrC < endRowPtrC) )
{
endColPtrA = *rowPtrA + COLS;
endColPtrB = *rowPtrB + COLS;
endColPtrC = *rowPtrC + COLS;
colPtrA = *rowPtrA;
colPtrB = *rowPtrB;
colPtrC = *rowPtrC;
while( (colPtrA < endColPtrA) && (colPtrB < endColPtrB) && (colPtrC < endColPtrC) )
{
*colPtrC = *colPtrA + *colPtrB;
colPtrA++;
colPtrB++;
colPtrC++;
}
rowPtrA++;
rowPtrB++;
rowPtrC++;
}
return matrixC;
}
int** TransposeMatrix(int** matrix, const int ROWS, const int COLS)
{
//Allocate the memory of the Transpose Matrix dynamically on Heap
int** matrixD = NULL;
matrixD = AllocateMatrix (COLS , ROWS);
int** rowPtr = matrix;
int* colPtr = *rowPtr ;
int** endRowPtr = matrix + ROWS;
int* endColPtr = *rowPtr + COLS;
int i = 0;
int** rowPtrD = matrixD;
int* colPtrD = *rowPtrD;
int** endRowPtrD = matrixD + COLS;
int* endColPtrD = *rowPtrD + ROWS;
while ( rowPtrD < endRowPtrD )
{
rowPtr = matrix;
colPtrD = *rowPtrD;
while ( rowPtr < endRowPtr )
{
colPtr = *rowPtr + i;
*colPtrD = *colPtr;
colPtrD++;
rowPtr++;
}
i++;
rowPtrD++;
}
return matrixD;
}
bool IsSymmetric(int** matrix, const int ROWS, const int COLS)
{
bool isSymmetric = true;
for (int i = 0 ; i < ROWS && isSymmetric ; i++)
{
for (int j = 0 ; j < COLS && isSymmetric ; j++)
{
if ( matrix[i][j] != matrix[j][i] )
isSymmetric = false;
}
}
return isSymmetric;
}
void InterchangeRows(int** matrix, const int ROWS, const int COLS )
{
int rowOne , rowTwo = 0;
cout << endl << "Enter row1 : ";
cin >> rowOne;
while( rowOne <= 0 || rowOne > ROWS)
{
cout << "You have entered an invalid Row Number . Enter a valid Row1 :"<< endl;
cin >> rowOne;
}
cout << "Enter row2 : ";
cin >> rowTwo;
while( rowTwo <= 0 || rowTwo > ROWS)
{
cout << "You have entered an invalid Row Number . Enter a valid Row2 :"<< endl;
cin >> rowTwo;
}
int* row1 = *( matrix + (rowOne - 1) );
int* row2 = *( matrix + (rowTwo - 1) );
InterchangeRows (row1 , row2);
*( matrix + (rowOne - 1) ) = row1;
*( matrix + (rowTwo - 1) ) = row2;
}
void InterchangeRows( int* &row1, int* &row2 )
{
int* temp = NULL;
temp = row1;
row1 = row2;
row2 = temp;
}
void deAllocateMatrix(int** &matrix, const int ROWS)
{
if(matrix)
{
for ( int i = 0 ; i < ROWS ; i++)
delete[] matrix[i];
delete[] matrix;
}
matrix = NULL;
}
int main()
{
int rowsA, colsA, rowsB, colsB = 0 ;
int** matrixA = NULL;
int** matrixB = NULL;
int** matrixC = NULL; //Matrix after addition of Matrix A and Matrix B
int** matrixAT = NULL; //Transpose of Matrix A
int** matrixBT = NULL; //Transpose of Matrix B
//Code to call Input And Output Functions to get Matrices from user and display them
cout << "Enter Matrix A : " << endl;
matrixA = InputMatrix (rowsA , colsA);
cout << endl <<"Enter Matrix B : " << endl;
matrixB = InputMatrix (rowsB, colsB);
cout << endl << "Matix A = ";
OutputMatrix (matrixA , rowsA , colsA);
cout << endl << "Matrix B = ";
OutputMatrix (matrixB , rowsB , colsB);
/////Code to call the AddMatrix Function and Display the resultant Matrix i.e MatrixC
if ( (rowsA == rowsB) && (colsA == colsB) )
{
matrixC = AddMatrix (matrixA, matrixB ,rowsA ,colsA); // becuase rowsA and rowsB are same and colsA and colsB are same.
cout << endl << "A + B = " ;
OutputMatrix (matrixC , rowsA , colsA); //becuase matrix C will have the same rows and columns as that of Matrix A and Matrix B
}
else
cout <<endl<< "Matrices CANNOT be ADDED as they are of DIFFERENT SIZE. "<<endl;
//////Code to call the TranposeMatrix Function and Display the resultant Matrices i.e MatrixAT and MatrixBT
cout << endl << "Transpose of A =";
matrixAT = TransposeMatrix (matrixA , rowsA ,colsA);
OutputMatrix (matrixAT , colsA ,rowsA);
cout << endl << "Transpose of B =";
matrixBT = TransposeMatrix (matrixB , rowsB ,colsB);
OutputMatrix (matrixBT, colsB ,rowsB);
///////Code to call the IsSymmetric Function to check whether the matrices are Symmetric or NOT
if (rowsA != colsA)
cout <<endl<< "Since the MatrixA is not a square matrix." <<endl<<"So by definiton , it can NEVER be a Symmetric Matrix";
else
{
if( IsSymmetric(matrixA , rowsA , colsA))
cout <<endl<< "Matrix A is Symmetric. " << endl;
else
cout <<endl << "Matrix A is NOT Symmetric. "<< endl;
}
if (rowsB != colsB)
cout <<endl<< "Since the MatrixB is not a square matrix." <<endl<<"So by definiton , it can NEVER be a Symmetric Matrix"<<endl;
else
{
if ( IsSymmetric(matrixB , rowsB , colsB) )
cout <<endl<< "Matrix B is Symmetric. " << endl;
else
cout <<endl << "Matrix B is NOT Symmetric. "<< endl;
}
//////Code to call the InterchangeRows Function and display the results
cout <<endl << "Interchanging rows of Matrix A :";
InterchangeRows(matrixA , rowsA , colsA );
cout <<endl << "After Interchanging Rows Matrix A =";
OutputMatrix (matrixA, rowsA , colsA);
cout <<endl << "Interchanging rows of Matrix B :";
InterchangeRows(matrixB , rowsB , colsB );
cout <<endl << "After Interchanging Rows Matrix B =";
OutputMatrix (matrixB , rowsB , colsB);
////Code to de-allocate all the Matrices
deAllocateMatrix(matrixA , rowsA);
cout<< endl<< "MatrixA has been succesfully de-allocated .";
deAllocateMatrix(matrixB , rowsB);
cout <<endl<< "MatrixB has been succesfully de-allocated .";
deAllocateMatrix(matrixC , rowsA);
cout <<endl<< "MatrixC has been succesfully de-allocated .";
deAllocateMatrix(matrixAT , colsA);
cout <<endl<< "MatrixAT has been succesfully de-allocated .";
deAllocateMatrix(matrixBT , colsB);
cout <<endl<< "MatrixBT has been succesfully de-allocated .";
//system("pause");
return 0;
}