#include<iostream>
#include<math.h>
#include<conio.h>
using namespace std;
double d=0;
double det(int n,double mat[10][10]); // u can replace 10 with any number
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();
}
double det(int n,double mat[10][10])
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; //submatrix's i value
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;
}
#include<math.h>
#include<conio.h>
using namespace std;
double d=0;
double det(int n,double mat[10][10]); // u can replace 10 with any number
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();
}
double det(int n,double mat[10][10])
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; //submatrix's i value
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;
}
d=d+(pow(-1,c)*mat[0][c]*det(n-1 ,submat)); how this line works can u explain me??
ReplyDeleteThis code looks like a pile of trash!
ReplyDeleteMany many issues with this code. Lots of the variables haven't been declared, such as subi, subj, i, j. Variables i and j also aren't used, and yet subi and subj are used as counters within the for loops - just use subi and subj as the loop counters!!
ReplyDeleteReason to believe that this code is stolen from
http://www.sanfoundry.com/cpp-program-find-determinant-given-matrix/
and very marginally rearranged, if anybody wants to use this algorithm look there for a version which will actually compile.