// inverse of a 2 by 2 matrix
#include<iostream.h>
void main()
{
float a[2][2], inv[2][2], d, temp;
int i, j;
// initializing 2 D array a with user defined values
cout<<"Enter values for array A"<<endl<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<"enter value for a["<<i<<"]["<<j<<"]:";
cin>>a[i][j];
}
}
// origional array A
cout<<"displaying origional array A:"<<endl<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
// determinant of A
d = a[0][0]*a[1][1] - a[0][1]*a[1][0];
cout<<"determinant of A is "<<d<<endl<<endl;
// adjoint of A
temp = a[0][0];
a[0][0] = a[1][1];
a[1][1] = temp;
a[1][0] = -1*a[1][0];
a[0][1] = -1*a[0][1];
// origional array A
cout<<"displaying origional array A:"<<endl<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
// inverse of A
cout<<"displaying inverse of array A:"<<endl<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
inv[i][j] = a[i][j]/d;
cout<<inv[i][j]<<" ";
}
cout<<endl;
}
}
#include<iostream.h>
void main()
{
float a[2][2], inv[2][2], d, temp;
int i, j;
// initializing 2 D array a with user defined values
cout<<"Enter values for array A"<<endl<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<"enter value for a["<<i<<"]["<<j<<"]:";
cin>>a[i][j];
}
}
// origional array A
cout<<"displaying origional array A:"<<endl<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
// determinant of A
d = a[0][0]*a[1][1] - a[0][1]*a[1][0];
cout<<"determinant of A is "<<d<<endl<<endl;
// adjoint of A
temp = a[0][0];
a[0][0] = a[1][1];
a[1][1] = temp;
a[1][0] = -1*a[1][0];
a[0][1] = -1*a[0][1];
// origional array A
cout<<"displaying origional array A:"<<endl<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<endl;
// inverse of A
cout<<"displaying inverse of array A:"<<endl<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
inv[i][j] = a[i][j]/d;
cout<<inv[i][j]<<" ";
}
cout<<endl;
}
}
0 comments:
Post a Comment