Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define N 3
- void cofactor(int mat[N][N], int temp[N][N], int p, int q, int n) {
- int i = 0, j = 0;
- for (int row = 0; row < n; row++) {
- for (int col = 0; col < n; col++) {
- if (row != p && col != q) {
- temp[i][j++] = mat[row][col];
- if (j == n - 1) {
- j = 0;
- i++;
- }
- }
- }
- }
- }
- int determinant(int mat[N][N], int n) {
- int D = 0;
- if (n == 1)
- return mat[0][0];
- int temp[N][N];
- int sign = 1;
- for (int f = 0; f < n; f++) {
- cofactor(mat, temp, 0, f, n);
- D += sign * mat[0][f] * determinant(temp, n - 1);
- sign = -sign;
- }
- return D;
- }
- int main() {
- // A se seta N-ul ca si constanta la inceputul prgramului
- // Am scris matricea din exercitiu intr-o lista de liste denumita mat
- int mat[N][N] = {{1, -3, 1},
- {2, 3, -1},
- {1, -1, -1}};
- cout << "Determinantul matrice mat este: " << determinant(mat, N);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement