C++ Program to Calculate the Inverse of matrix. This program finds the inverse of a matrix and prints the result on the
compiler screen. We define a 3-dimensional array 'a' of int type. It is
input by the user. The number of rows and columns are made fixed as 3.
Firstly determinant of the matrix is calculated using nested for loops
and if determinant is equal to zero inverse does not exists and error message is displayed otherwise inverse is printed on the compiler screen.
Source Code:-
#include<iostream.h>
#include<conio.h>
void main()
{int a[3][3],i,j;
float determinant=0;
clrscr();
cout<<"Find Inverse Of Matrix by-Tarun Rawat\n";
cout<<"Enter elements of 3x3 matrix:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"\nThe entered matrix is:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
for(i=0;i<3;i++)
{
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*
a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
}
if(determinant==0)
{
cout<<"Inverse does not exist (Determinant=0).\n";
}
else
{
cout<<"\nInverse of matrix is: \n";
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<(a[(i+1)%3][(j+1)%3] *
a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*
a[(i+2)%3][(j+1)%3]))/ determinant<<"\t";
}
cout<<"\n";
}
getch();
}
and if determinant is equal to zero inverse does not exists and error message is displayed otherwise inverse is printed on the compiler screen.
Source Code:-
#include<iostream.h>
#include<conio.h>
void main()
{int a[3][3],i,j;
float determinant=0;
clrscr();
cout<<"Find Inverse Of Matrix by-Tarun Rawat\n";
cout<<"Enter elements of 3x3 matrix:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"\nThe entered matrix is:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
for(i=0;i<3;i++)
{
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*
a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
}
if(determinant==0)
{
cout<<"Inverse does not exist (Determinant=0).\n";
}
else
{
cout<<"\nInverse of matrix is: \n";
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<(a[(i+1)%3][(j+1)%3] *
a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*
a[(i+2)%3][(j+1)%3]))/ determinant<<"\t";
}
cout<<"\n";
}
getch();
}
And how to do the same way for 2x2, 4x4 and 5x5? I would appreciate your help
ReplyDelete