Print Friendly and PDF
#include<iostream> 
#include<cmath> 
using namespace std; 
int det(int n,int**ptr) 
{ 
if(n==1) 
return **ptr; 
else 
{ 
int a[n][n],b[n-1][n-1],*c[n-1],**d,**p,**pt,e=0; 
p=ptr; 
for(int i=0;i<n;i++) 
{ 
for(int j=0;j<n;j++) 
{
a[i][j]= **p; 
++(*p); 
}
++p; 
} 
for(int i=0;i<n;i++) 
{ 
--p; 
for(int j=0;j<n;j++) 
--(*p); 
} 
for(int k=0;k<n;k++) 
{ 
for(int i=0;i<n-1;i++) 
{ 
for(int j=0;j<n-1;j++) 
{ 
if(j<k) 
b[i][j]=a[i+1][j]; 
else
 b[i][j]=a[i+1][j+1]; 
} 
} 
for(int i=0;i<n-1;i++) 
c[i]=b[i]; 
d=c; 
e=e+pow(-1,k)*a[0][k]*det(n-1,d); 
//cout<<e<<endl; 
} 
return e; 
} 
} 
void inverse(int n,int**ptr) 
{ 
int a[n][n],b[n][n],c[n-1][n-1],*d[n-1],**e,**p,**h,f; 
p=ptr; 
f=det(n,p); 
p=ptr; 
cout<<f<<endl; 
for(int i=0;i<n;i++) 
{ 
for(int j=0;j<n;j++) 
{
a[i][j]= **p; 
++(*p); 
cout<<a[i][j]<<" "; 
} 
++p; 
cout<<endl; 
}
cout<<endl; 
for(int i=0;i<n;i++) 
{ 
for(int j=0;j<n;j++) 
{ 
for(int k=0;k<n-1;k++) 
{
for(int l=0;l<n-1;l++) 
{ 
if(k<i&&l<j)
 c[k][l]=a[k][l]; 
else 
if(k>=i&&l<j) c[k][l]=a[k+1][l]; 
else 
if(k<i&&l>=j) c[k][l]=a[k][l+1]; 
else 
c[k][l]=a[k+1][l+1]; 
} 
} 
for(int k=0;k<n-1;k++) 
d[i]=c[i]; 
e=d;h=e; 
cout<<det(n-1,e)<<" "; 
b[i][j]=pow(-1,i)*pow(-1,j)*det(n-1,e); 
cout<<b[i][j]/(1.0*f)<<" "; 
}
cout<<endl; 
} 
}
int main() 
{ 
int n; 
cout<<"enter dimension "; 
cin>>n; 
int a[n][n],*b[n],**c; 
cout<<"enter matrix\n"; 
for(int i=0;i<n;i++) 
{
for(int j=0;j<n;j++) 
cin>>a[i][j]; 
} 
for(int i=0;i<n;i++) 
b[i]=a[i]; 
c=b; 
cout<<"inverse is\n"; 
inverse(n,c); 
}
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: