C++ program for finding determinant value and Inverse of a Matrix

 C++ program for finding determinant value and Inverse of a Matrix (3x3)

-Dinesh Kumar E


This is a simple C++ program to find Determinant value and inverse of matrix.


Code:

#include<iostream>
#include<iomanip>
using namespace std;
int m[10],j,ch,det,i[10];
int main()
{
cout<<"To find Inverse of matrix"<<endl<<"Enter the correct option"<<endl<<"1 for  2x2 matrix\n2 for 3x3 matrix"<<endl;cin>>ch
switch(ch)
case 1:
{for(j=0;j<9;j++)
{
cout<<"Enter no "<<j+1<<" element ";
cin>>m[j];
}
det=((m[4]*m[8]-m[7]*m[5])*m[0])-((m[3]*m[8]-m[6]*m[5])*m[1])+((m[3]*m[7]-m[6]*m[4])*m[2]);
if(det!=0)
{
i[0]=1*(m[4]*m[8]-m[7]*m[5]);
i[1]=-1*(m[3]*m[8]-m[6]*m[5]);
i[2]=1*(m[3]*m[7]-m[6]*m[4]);
i[3]=-1*(m[1]*m[8]-m[7]*m[2]);
i[4]=1*(m[0]*m[8]-m[6]*m[2]);
i[5]=-1*(m[0]*m[7]-m[6]*m[1]);
i[6]=1*(m[1]*m[5]-m[4]*m[2]);
i[7]=-*(m[0]*m[4]-m[3]*m[1]);
cout<<1*(m[0]*m[5]-m[3]*m[2]);
i[8]=1"\n\nThe inverse of entered matrix is :"<<endl;
cout<<"| "<<i[0]<<" "<<i[3]<<" "<<i[6]<<"|";
cout<<setw(8)<<" * "<<" 1/"<<det<<endl;
cout<<"| "<<i[1]<<" "<<i[4]<<" "<<i[7]<<"|"<<endl;
cout<<"| "<<i[2]<<" "<<i[5]<<" "<<i[8]<<"|"<<endl;
}
else
cout<<"\n\nThis matrix don't have inverse because its determine vale is "<<det;}
case 2:
for(j=0;j<4;j++)
{
cout<<"Enter no "<<j+1<<" element ";
cin>>m[j];
}

}

Comments

Popular Posts