-5

I have found a program on internet which calculate the determinant of a Matrix:

 /*
 * C++ Program to Find the Determinant of a Given Matrix
 */
#include<iostream>
#include<math.h>
#include<conio.h>
using namespace std;
double d = 0;
double det(int n, double mat[10][10])
{
    int c, subi, i, j, subj;
    double submat[10][10];  
    if (n == 2) 
    {
        return( (mat[0][0] * mat[1][1]) - (mat[1][0] * mat[0][1]));
    }
    else
    {  
        for(c = 0; c < n; c++)
        {  
            subi = 0;  
            for(i = 1; i < n; i++)
            {  
                subj = 0;
                for(j = 0; j < n; j++)
                {    
                    if (j == c)
                    {
                        continue;
                    }
                    submat[subi][subj] = mat[i][j];
                    subj++;
                }
                subi++;
            }
        d = d + (pow(-1 ,c) * mat[0][c] * det(n - 1 ,submat));
        }
    }
    return d;
}
int main()
{
    int n;
    cout<<"enter the order of matrix" ;
    cin>>n;
    double mat[10][10];
    int i, j;
    cout<<"enter the elements"<<endl;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            cin>>mat[i][j];
        }
    }
    cout<<"\ndeterminant"<<det(n,mat);
    getch();
}

source: http://www.sanfoundry.com/cpp-program-find-determinant-given-matrix/

I wanted to learn from it but i don't understand it. Is it any link with Gauss elimination? Otherwise do you know which process use this algorithm?

Thank you in advance to any one who may be able to help me

Muclos
  • 1
  • 2

2 Answers2

0

This is an algorithm which uses Lapace expansions, which recursively computes the determinant of an n x n matrix by computing n determinants of (n-1) x (n-1) subminors. The determinant of the 2 x 2 matrix should be obvious.

There are better ways to do that, like LU decomposition.

Dorian Gray
  • 2,913
  • 1
  • 9
  • 25
0

The program uses a recursive function to create a submatrix and compute the determinant when the submatrix is 2x2.

When the program has the determinant of the submatrix it sums and subtracts it as you can see on Wikipedia page about determinant.

At the end, the recursive function returns the determinant of the complete matrix.