Given a square matrix. We must find its determinant. Where exactly was I wrong?

#include <iostream> #include <time.h> using namespace std; class Matrix { int** mas; int n; void init() { mas = new int*[n]; for (int i = 0; i < n; i++) { mas[i] = new int[n]; } } void fill() { for (int i = 0; i <n; i++) { for (int j = 0; j < n; j++) { mas[i][j] = rand() % 10; } } } public: Matrix(int x, int y) { n = x; n = y; init(); fill(); }; void print() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << mas[i][j] << ' '; } cout << endl; } } void opred() { int p=0, k,t; for (int i = 0; i < n-1; i++) { t=1; while(mas[i][i]==0) { // переставить i-ую и (i + t)-ую строки for (int j = 0; j < n; j++) { mas[i][j]=k; mas[i][j]=mas[i+t][j]; mas[i+t][j]=k; } p++; t++; } for (int m=i+1; m<n; m++) { // вычесть из m-ой строки i-ую так, чтобы создать 0 // под клеткой (i, i) k=mas[m][i]/mas[i][i]; for(int j=0; j<n; j++) { mas[m][j]-=mas[i][j]*k; } } } k = pow(-1.0, p); for(int i=0; i<n; i++) { k*=mas[i][i]; } cout<<"Определитель матрицы ="<<k<<endl; } }; int main() { srand(time(0)); setlocale(LC_ALL, "Russian"); int x, y; cout << "Введите количество строк: "; cin >> y; cout << "Введите количество столбцов: "; cin >> x; Matrix a(x,y); a.print(); a.opred(); system ("pause"); return 0; } 
  • What method do you use? Gauss is not watching here like. > opred This is not professional. It would be more logical to call determinant , or getDeterminant() - Opalosolo
  • Who knows where the error. For example, in mas[i][j]=k; - what do you think k ? - VladD
  • Then, the cycle while(mas[i][i]==0) - where is the guarantee that it will end? (For example, if the matrix is ​​all zero, what will happen?) - VladD
  • @ ua6xh, this is exactly an attempt to calculate according to Gauss. But a bad try. In the class there is no necessary in this case destructor. - BuilderC
  • one
    @BuilderC: Well, imagine. You took someone from the class Matrix and do not know what's inside. Put there elements. Calculate the determinant. And suddenly the elements themselves "jumped." Absurd, right? Well, you did not ask to change the elements, you hope that where you put them, there they will be, and that a simple determinant calculation is a non-destructive operation. Here it is. - VladD

1 answer 1

  1. I am afraid that your determinant will often be zero, since the elements of the matrix are int, and with integer division of the smaller by the larger ... But double would be necessary. You can, of course, with integers, but you will have to create a CRUSH class with numerators and denominators.

  2. Search for the main line not by the zero value of the element, but by the maximum absolute value of it.

  3. I do not see release of memory in your code.

Just because I have a granddaughter Masha:

 double TMatrix::Determinant() { double d = 1.0; for(int i = 0; i < line; i++) { //Поиск главной строки int lmax = i; for(int j = i + 1; j < line; j++) lmax = fabs(m[j][i]) > fabs(m[lmax][i])? j: lmax; if(lmax > i) { SwapLines(lmax, i); d = -d; } d *= m[i][i]; if(fabs(d) < 1.0e-10) return 0.0; //Конец поиска главной строки //Начало прямого хода for(int j = i + 1; j < col; j++) { m[i][j] /= m[i][i]; //единственное деления for(int k = i + 1; k < line; k++) m[k][j] -= m[i][j] * m[k][i]; } //Конец прямого хода } return d;