determinant
Abstract
This is a program designed for finding the determinant of any
matrix.
It uses depth-first search to find the Laplace expansion form of each
matrix.
#include<bits/stdc++.h>
using namespace std;
struct mx{
int m,n;
int qu[100][100];
};
int det(mx U){
if(U.m==1){
return U.qu[1][1];
}
if(U.m==2){
int mid;
mid=U.qu[1][1]*U.qu[2][2]-U.qu[1][2]*U.qu[2][1];
return mid;
}
if(U.m>=3){
int allmid=0;
for(int i=1;i<=U.n;i++){
mx subU;
subU.m=U.m-1;
subU.n=U.n-1;
int flag=0;
for(int ii=2;ii<=U.m;ii++){
flag=0;
for(int jj=1;jj<=U.n;jj++){
if(jj==i)
continue;
flag++;
subU.qu[ii-1][flag]=U.qu[ii][jj];
}
}
if(i%2==0)
allmid-=det(subU)*U.qu[1][i];
else
allmid+=det(subU)*U.qu[1][i];
}
return allmid;
}
}
int main(){
mx A;
cout<<"Please enter the size of the determinant."<<endl;
cout<<"Please enter a space between them."<<endl;
cin>>A.m>>A.n;
cout<<"Please enter the elements (matrix form)."<<endl;
for(int i=1;i<=A.m;i++){
for(int j=1;j<=A.n;j++){
cin>>A.qu[i][j];
}
}
for(int i=1;i<=A.m;i++){
cout<<"| ";
for(int j=1;j<=A.n;j++){
cout<<A.qu[i][j]<<" ";
}
if(i!=A.m)
cout<<"|"<<endl;
else
cout<<"|";
}
cout<<" = ";
if(A.m!=A.n) cout<<"no result !!!"<<endl;
else cout<<det(A)<<endl;
return 0;
}