+ 5
Program to find out determinant of n*n matrix in C or C++?
2 odpowiedzi
+ 3
the solution is simple first we will divide the big matrix into smaller matrices then multiply the determinant we got the cofactors
here sign is important
the determinant of small matrices is by further dividing if of high order and cross multiply and subtract. if of order 2 we stop at order 2
#include <iostream .h>
#include <math.h>
double determinant=0;
double det( int n, double mat)
{.
// this the function that calculate determinant of matrix by putting order at one argument and the matrix other
int c, subi, subj, i,
j
double submit[10][10]
// that's it divide the matrix into small matrix and calculate it by cross multiply and subtracting
if (n==2)
return mat[0]*mat[1][1]-mat[1][0]*mat[0][1];
else{
for .( i=0; i<n;i++){
for(j=0;j<n;j++)
{
if(j==c) continue
submat[subi][subj]=mat[i][j]
subj++}subi++
}
determinant+=pow(-1,c)*mat[0][c]*det(n-1,submat);
// determinant is signwithcofactor*determinantsofsmallmatricesoflessorder
}
}
return determinant;
int main()
//you can use the function here
cout<<"determinant of matrix"<<det(n, mat);
// you can let user enter the matrix see my other posts to find how to do that and store it in n and mat
}
+ 2
thank you so much
I was trying to make it from the weeks.