Advertisement
SKREFI

Liviu19Ex3

Jan 19th, 2021 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define N 3
  5.  
  6. void cofactor(int mat[N][N], int temp[N][N], int p, int q, int n) {
  7.     int i = 0, j = 0;
  8.     for (int row = 0; row < n; row++) {
  9.         for (int col = 0; col < n; col++) {
  10.             if (row != p && col != q) {
  11.                 temp[i][j++] = mat[row][col];
  12.                 if (j == n - 1) {
  13.                     j = 0;
  14.                     i++;
  15.                 }
  16.             }
  17.         }
  18.     }
  19. }
  20.  
  21. int determinant(int mat[N][N], int n) {
  22.     int D = 0;
  23.  
  24.     if (n == 1)
  25.         return mat[0][0];
  26.  
  27.     int temp[N][N];
  28.  
  29.     int sign = 1;
  30.  
  31.     for (int f = 0; f < n; f++) {
  32.         cofactor(mat, temp, 0, f, n);
  33.         D += sign * mat[0][f] * determinant(temp, n - 1);
  34.         sign = -sign;
  35.     }
  36.  
  37.     return D;
  38. }
  39.  
  40. int main() {
  41.     // A se seta N-ul ca si constanta la inceputul prgramului
  42.  
  43.     // Am scris matricea din exercitiu intr-o lista de liste denumita mat
  44.     int mat[N][N] = {{1, -3, 1},
  45.                      {2, 3, -1},
  46.                      {1, -1, -1}};
  47.  
  48.     cout << "Determinantul matrice mat este: " << determinant(mat, N);
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement