Skip to content

Commit 4e5a430

Browse files
committed
moar!
- added a UTIL function to generate random int flattened 2d array - added some more info and cleaned up some method definitions
1 parent f730c87 commit 4e5a430

File tree

1 file changed

+78
-31
lines changed

1 file changed

+78
-31
lines changed

ADT/miscellaneous/matrix.cpp

Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@
22
#include<omp.h>
33
#include<stdexcept>
44

5+
6+
//extra imports for utility
7+
#include<random>
8+
59
using namespace std;
610

711
template<typename DATA>
812
class matrix {
9-
/*
10-
val = flattened 2d array of type DATA in row major form
11-
row = number of rows of the matrix
12-
col = number of columns of the matrix
13-
14-
Note: memory is dynamically allocated for the `val`
15-
member variable. It's lifetime is until
16-
the object instance of `matrix` defining it is alive.
13+
/*
14+
matrix abstract DS reflects properties and behaviour of a matrix
15+
16+
DATA MEMEBERS:-
17+
val = flattened 2d array of type DATA in row major form
18+
row = number of rows of the matrix
19+
col = number of columns of the matrix
20+
21+
Note:
22+
1. memory is dynamically allocated for the `val`
23+
member variable. It's lifetime is until
24+
the object instance of `matrix` defining it is alive.
25+
2. This class object makes use of openMP paralleisation to
26+
take advantage of parallel computing in some operations
27+
such as matrix multiplication.
1728
*/
1829
DATA *val;
1930
int row, col;
@@ -240,7 +251,7 @@ matrix<DATA> matrix<DATA>::operator-(matrix const& obj) {
240251
template<typename DATA>
241252
void matrix<DATA>::insertAll() {
242253
int i,j;
243-
cout<<"\nNote: you have to insert "<<this->row*this->col<<" values. Values will be filled row-major wise.\n";
254+
cout<<"\nNote: you have to insert "<<this->row*this->col<<" values. Values will be filled row-major wise in a "<<this->row<<'x'<<this->col<<" matrix.\n";
244255
for(i=0; i<this->row; i++)
245256
for(j=0; j<this->col; j++)
246257
cin>>*(val + (this->col)*i + j);
@@ -282,38 +293,74 @@ void init2dArray(int *array, int size_0, int size_1) {
282293
cin>>*(array + i*size_1 + j);
283294
}
284295

296+
void init2dRandArray(int *array, int size_0, int size_1) {
297+
/*
298+
UTIL FUNCTION
299+
Flattened 2d array in row major form will be initialised using a
300+
uniform integer distribution.
301+
*/
302+
303+
cout<<"\nInitializing our random 2d integer array";
304+
std::default_random_engine generator;
305+
std::uniform_int_distribution<int> distribution(0, 9);
306+
307+
for(int i=0; i<size_0; i++)
308+
for(int j=0; j<size_1; j++)
309+
*(array + i*size_1 + j) = distribution(generator);
310+
}
311+
285312
int main() {
286313

287-
int row=2,
288-
col=3,
289-
row2=3,
290-
col2=2;
314+
// int row=2,
315+
// col=3,
316+
// row2=3,
317+
// col2=2;
291318

292-
int *array1 = new int[row*col];
293-
int *array2 = new int[row2*col2];
294-
init2dArray(array1, row, col); // 2x3 array
295-
init2dArray(array2, row2, col2); // 3x2 array
319+
// int *array1 = new int[row*col];
320+
// int *array2 = new int[row2*col2];
321+
// init2dArray(array1, row, col); // 2x3 array
322+
// init2dArray(array2, row2, col2); // 3x2 array
296323

297-
matrix<int> m1(array1, row, col); //2x3 matrix
298-
matrix<int> m2(array2, row2, col2); //3x2 matrix
324+
// matrix<int> m1(array1, row, col); //2x3 matrix
325+
// matrix<int> m2(array2, row2, col2); //3x2 matrix
299326

300-
matrix<int> m3 = m1 & m2;
327+
// matrix<int> m3 = m1 & m2;
328+
329+
// m1.display();
330+
// m2.display();
331+
332+
// cout<<"\nMatrix Multiplication Result:-\n";
333+
// m3.display();
334+
335+
// matrix<int> m4 = m3*2;
336+
// m4.display();
337+
338+
// delete array1;
339+
// delete array2;
340+
// array1 = NULL;
341+
// array2 = NULL;
342+
301343

302-
m1.display();
303-
m2.display();
344+
int r = 10, c = 10;
345+
int *arr1 = new int[r*c], *arr2= new int[r*c];
346+
init2dRandArray(arr1, r, c);
347+
init2dRandArray(arr2, r, c);
304348

305-
cout<<"\nMatrix Multiplication Result:-\n";
306-
m3.display();
349+
matrix<int> m10(arr1, r, c);
350+
matrix<int> m11(arr2, r, c);
351+
matrix<int> m12 = m10 & m11;
307352

308-
matrix<int> m4 = m3*2;
309-
m4.display();
353+
m10.display();
354+
m11.display();
355+
cout<<"\nMatrix Mul Result:-\n";
356+
m12.display();
310357

311-
delete array1;
312-
delete array2;
313-
array1 = NULL;
314-
array2 = NULL;
358+
delete arr1;
359+
delete arr2;
360+
arr1 = NULL;
361+
arr2 = NULL;
315362

316-
return 1;
363+
return 0;
317364
}
318365

319366

0 commit comments

Comments
 (0)