Vla_DOS

Untitled

Oct 15th, 2022
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. const int n = 4;
  7. int temp_matrix[n][n];
  8.  
  9. void printMtx(int matrix[n][n])
  10. {
  11.     for (int i = 0; i < n; i++)
  12.     {
  13.         for (int j = 0; j < n; j++)
  14.             cout << matrix[i][j] << " ";
  15.         cout << "\n";
  16.     }
  17. }
  18.  
  19. void get_matr(int matrix[n][n], int kolvo, int i, int j)
  20. {
  21.     int ki, kj, di, dj;
  22.     di = 0;
  23.     for (ki = 0; ki < kolvo - 1; ki++)
  24.     {
  25.         if (ki == i) di = 1;
  26.         dj = 0;
  27.         for (kj = 0; kj < kolvo - 1; kj++)
  28.         {
  29.             if (kj == j) dj = 1;
  30.             temp_matrix[ki][kj] = matrix[ki + di][kj + dj];
  31.         }
  32.     }
  33. }
  34.  
  35. int det(int matrix[n][n], int count)
  36. {
  37.     int temp = 0;
  38.     int k = 1;
  39.  
  40.     if (count < 1) {
  41.         cout << "not run";
  42.         return 0;
  43.     }
  44.     else if (count == 1)
  45.         temp = matrix[0][0];
  46.     else if (count == 2)
  47.         temp = matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1];
  48.     else
  49.     {
  50.         for (int i = 0; i < count; i++)
  51.         {
  52.             get_matr(matrix, count, i, 0);
  53.  
  54.             temp = temp + k * matrix[i][0] * det(temp_matrix, count - 1);
  55.             k = -k;
  56.         }
  57.     }
  58.     return temp;
  59. }
  60.  
  61. void Rand(int mtx[n][n], int n) {
  62.     for (int i = 0; i < n; i++)
  63.         for (int j = 0; j < n; j++)
  64.             mtx[i][j] = (int)(rand() % 22);
  65. }
  66.  
  67. int main()
  68. {
  69.     int mtx[n][n];
  70.     int i, j;
  71.     int det_res;
  72.     Rand(mtx, n);
  73.  
  74.     printMtx(mtx);
  75.  
  76.  
  77.     det_res = det(mtx, n);
  78.     cout << "Determinant = " << det_res << endl;
  79.  
  80.     int A[n][n];
  81.     Rand(A, n);
  82.  
  83.     printMtx(A);
  84.     int Adet_res = det(A, n);
  85.     cout << "Determinant = " << Adet_res << endl;
  86.  
  87.     if (det_res > Adet_res) {
  88.         cout << "Max Determinant = " << det_res << endl;
  89.     }
  90.     else {
  91.         cout << "Max Determinant = " << Adet_res << endl;
  92.  
  93.     }
  94.    
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment