Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- using namespace std;
- const int n = 4;
- int temp_matrix[n][n];
- void printMtx(int matrix[n][n])
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- cout << matrix[i][j] << " ";
- cout << "\n";
- }
- }
- void get_matr(int matrix[n][n], int kolvo, int i, int j)
- {
- int ki, kj, di, dj;
- di = 0;
- for (ki = 0; ki < kolvo - 1; ki++)
- {
- if (ki == i) di = 1;
- dj = 0;
- for (kj = 0; kj < kolvo - 1; kj++)
- {
- if (kj == j) dj = 1;
- temp_matrix[ki][kj] = matrix[ki + di][kj + dj];
- }
- }
- }
- int det(int matrix[n][n], int count)
- {
- int temp = 0;
- int k = 1;
- if (count < 1) {
- cout << "not run";
- return 0;
- }
- else if (count == 1)
- temp = matrix[0][0];
- else if (count == 2)
- temp = matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1];
- else
- {
- for (int i = 0; i < count; i++)
- {
- get_matr(matrix, count, i, 0);
- temp = temp + k * matrix[i][0] * det(temp_matrix, count - 1);
- k = -k;
- }
- }
- return temp;
- }
- void Rand(int mtx[n][n], int n) {
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- mtx[i][j] = (int)(rand() % 22);
- }
- int main()
- {
- int mtx[n][n];
- int i, j;
- int det_res;
- Rand(mtx, n);
- printMtx(mtx);
- det_res = det(mtx, n);
- cout << "Determinant = " << det_res << endl;
- int A[n][n];
- Rand(A, n);
- printMtx(A);
- int Adet_res = det(A, n);
- cout << "Determinant = " << Adet_res << endl;
- if (det_res > Adet_res) {
- cout << "Max Determinant = " << det_res << endl;
- }
- else {
- cout << "Max Determinant = " << Adet_res << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment