Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- float Determinant(float **a,int n)
- {
- int i, j, j1, j2;
- float det = 0;
- float **m = NULL;
- if (n < 1) {
- return -1;
- } else if (n == 1) {
- det = a[0][0];
- } else if (n == 2) {
- det = a[0][0] * a[1][1] - a[1][0] * a[0][1];
- } else { // n > 2
- det = 0;
- for (j1 = 0; j1 < n; j1++) {
- m = new float* [n];
- for (i = 0; i < n-1; i++)
- m[i] = new float [n];
- for (i = 1; i < n; i++) {
- j2 = 0;
- for (j = 0; j < n; j++) {
- if (j == j1)
- continue;
- m[i-1][j2] = a[i][j];
- j2++;
- }
- }
- det += pow(-1.0,1.0+j1+1.0)*a[0][j1]*Determinant(m, n-1);
- for (i = 0; i < n - 1; i++)
- free(m[i]);
- free(m);
- }
- }
- return(det);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement