Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. float Determinant(float **a,int n)
  6. {
  7.    int i, j, j1, j2;
  8.    float det = 0;
  9.    float **m = NULL;
  10.  
  11.    if (n < 1) {
  12.        return -1;
  13.    } else if (n == 1) {
  14.       det = a[0][0];
  15.    } else if (n == 2) {
  16.       det = a[0][0] * a[1][1] - a[1][0] * a[0][1];
  17.    } else { // n > 2
  18.       det = 0;
  19.       for (j1 = 0; j1 < n; j1++) {
  20.          m = new float* [n];
  21.          for (i = 0; i < n-1; i++)
  22.             m[i] = new float [n];
  23.          for (i = 1; i < n; i++) {
  24.             j2 = 0;
  25.             for (j = 0; j < n; j++) {
  26.                if (j == j1)
  27.                   continue;
  28.                m[i-1][j2] = a[i][j];
  29.                j2++;
  30.             }
  31.          }
  32.          det += pow(-1.0,1.0+j1+1.0)*a[0][j1]*Determinant(m, n-1);
  33.          for (i = 0; i < n - 1; i++)
  34.             free(m[i]);
  35.          free(m);
  36.       }
  37.    }
  38.    return(det);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement