Last active
August 29, 2015 14:07
-
-
Save FatihBAKIR/433482dc3e82adedd57a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
class Matrix | |
{ | |
unsigned int Size; | |
double** Rows; | |
public: | |
Matrix(unsigned int Size); | |
~Matrix(); | |
void Fill(double* Elements); | |
void Set(int i, int j, double value); | |
double Get(int i, int j); | |
Matrix* Minor(int i, int j); | |
double GetDeterminant(); | |
void Print(); | |
}; | |
Matrix::Matrix(unsigned int Size) | |
{ | |
unsigned int i; | |
this->Size = Size; | |
this->Rows = new double*[Size]; | |
for (i = 0; i < Size; ++i) | |
this->Rows[i] = new double[Size]; | |
} | |
Matrix::~Matrix() | |
{ | |
unsigned int i; | |
for (i = 0; i < this->Size; ++i) | |
delete[] this->Rows[i]; | |
delete[] this->Rows; | |
} | |
void Matrix::Fill(double* Elements) | |
{ | |
unsigned int i, j; | |
for (i = 0; i < this->Size; ++i) | |
for (j = 0; j < this->Size; ++j) | |
this->Rows[i][j] = Elements[i * this->Size + j]; | |
} | |
void Matrix::Set(int i, int j, double value) | |
{ | |
this->Rows[i][j] = value; | |
} | |
double Matrix::Get(int i, int j) | |
{ | |
return this->Rows[i][j]; | |
} | |
void Matrix::Print() | |
{ | |
unsigned int i, j; | |
for (i = 0; i < this->Size; ++i) | |
{ | |
for (j = 0; j < this->Size; ++j) | |
std::cout << this->Rows[i][j] << " "; | |
std::cout << std::endl; | |
} | |
} | |
double Matrix::GetDeterminant() | |
{ | |
double minDet, mul, elem, Determinant; | |
Matrix* minor; | |
unsigned int j; | |
if (this->Size == 1) | |
return this->Get(0, 0); | |
Determinant = 0; | |
for (j = 0; j < this->Size; ++j) | |
{ | |
minor = this->Minor(0, j); | |
minDet = minor->GetDeterminant(); | |
mul = pow(-1, j); | |
elem = this->Get(0, j) * mul * minDet; | |
Determinant += elem; | |
delete minor; | |
} | |
return Determinant; | |
} | |
Matrix* Matrix::Minor(int x, int y) | |
{ | |
unsigned int i, j, n = 0; | |
Matrix* minor = new Matrix(this->Size - 1); | |
double* values = new double[(this->Size - 1) * (this->Size - 1)]; | |
for (i = 0; i < this->Size; ++i) | |
for (j = 0; j < this->Size; ++j) | |
if (x != i && y != j) | |
values[n++] = this->Get(i, j); | |
minor->Fill(values); | |
delete[] values; | |
return minor; | |
} | |
int main() | |
{ | |
Matrix m(5); | |
double elems[] = { | |
1, 0, 3, 8, 9, | |
0, 5, 0, 0, 1, | |
0, 2, 5, 0, 7, | |
1, 0, 0, 5, 3, | |
3, 2, 8, 0, 0 | |
}; | |
m.Fill(elems); | |
m.Print(); | |
std::cout << m.GetDeterminant() << std::endl; | |
std::cin >> elems[0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment