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; }
determinant, orgetDeterminant()- Opalosolomas[i][j]=k;- what do you thinkk? - VladDwhile(mas[i][i]==0)- where is the guarantee that it will end? (For example, if the matrix is all zero, what will happen?) - VladDMatrixand 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