-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path07_matrix_determinant.cpp
165 lines (136 loc) · 3.9 KB
/
07_matrix_determinant.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
// // Create a base class called Matrix. Use this class to store a matrix of 2 x 2 of int type values that could be used to calculate determinants and create matrices. Create class CalculateDeterminant which will calculate the determinant of a matrix. Using these classes, calculate the determinant of the matrix.
// // Header files
#include <iostream>
#include <iomanip>
// // use namespace
using namespace std;
// // define a class Matrix
class Matrix
{
public:
// // static member variables
static const int MAX_ROWS = 2;
static const int MAX_COLS = 2;
protected:
// // instance member variables
int **matrix, rows, cols;
public:
// // constructors
Matrix(int rows)
{
// // check for invalid rows
if (rows > MAX_ROWS)
rows = MAX_ROWS;
else if (rows < 0)
rows = -rows;
this->rows = rows;
this->cols = rows;
// // allocate memory dynamically
matrix = new int *[rows];
for (int i = 0; i < rows; i++)
matrix[i] = new int[cols];
}
Matrix(int rows, int cols)
{
// // check for invalid rows
if (rows > MAX_ROWS)
rows = MAX_ROWS;
else if (rows < 0)
rows = -rows;
// // check for invalid rows
if (cols > MAX_COLS)
cols = MAX_COLS;
else if (cols < 0)
cols = -cols;
this->rows = rows;
this->cols = cols;
// // allocate memory dynamically
matrix = new int *[rows];
for (int i = 0; i < rows; i++)
matrix[i] = new int[cols];
}
// // instance member variable to set element
void setElement(int element, int row, int col)
{
// // check for invalid indexes
if (rows < 0 || row > rows - 1 || col < 0 || col > cols - 1)
{
cout << "\n!!! Invalid Indexes...";
return;
}
matrix[row][col] = element;
}
// // instance member variable to input matrix
void inputMatrix()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << "\nEnter Element[" << i << "][" << j << "] => ";
cin >> matrix[i][j];
}
}
}
// // instance member variable to get element
int getElement(int row, int col)
{
// // check for invalid indexes
if (rows < 0 || row > rows - 1 || col < 0 || col > cols - 1)
{
cout << "\n!!! Invalid Indexes...";
return 0;
}
return matrix[row][col];
}
// // instance member variable to display matrix
void displayMatrix()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << left << setw(4) << matrix[i][j];
}
cout << endl;
}
}
// // destructor
~Matrix()
{
for (int i = 0; i < rows; i++)
delete[] matrix[i];
delete[] matrix;
}
};
// // define a class CalculateDeterminant by inheriting class Matrix
class CalculateDeterminant : public Matrix
{
public:
// // inheriting the constructor of the base class
using Matrix::Matrix;
// // instance member function to calculate determinant
int calDeterminant()
{
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
}
};
// // Main Function Start
int main()
{
// // create an instance of CalculateDterminant
CalculateDeterminant d1(2, 2);
// // set elements
d1.setElement(5, 0, 0);
d1.setElement(10, 0, 1);
d1.setElement(3, 1, 0);
d1.setElement(8, 1, 1);
// // display matrix
d1.displayMatrix();
// // calculate and display determinant of matrix
cout << "\nDeterminant of Matrix => " << d1.calDeterminant();
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End