In: Computer Science
Q in c++ Write a program that will find the inverse of any two dimensional array without the usage of the built-in function.? plz answer in while or do while loop
Points to consider:
Code
#include<iostream>
using namespace std;
int main(){
int mat[3][3], i, j;
float determinant = 0;
cout<<"Enter elements of matrix row
wise:\n";
i = 0;j=0;
while(i<3)
{
j = 0;
while(j<3)
{
cin>>mat[i][j];
j++;
}
i++;
}
printf("\nGiven matrix is:");
i = 0;j=0;
while(i<3){
cout<<"\n";
j=0;
while(j<3){
cout<<mat[i][j]<<"\t";
j++;
}
i++;
}
// This following is the calculation of the
determinant.
i = 0;
while(i<3)
{
determinant = determinant +
(mat[0][i] * (mat[1][(i+1)%3] * mat[2][(i+2)%3] - mat[1][(i+2)%3] *
mat[2][(i+1)%3]));
i++;
}
cout<<"\n\ndeterminant:
"<<determinant;
cout<<"\n\nInverse of matrix is: \n";
i = 0;j= 0;
while(i<3)
{
j= 0;
while(j<3)
{
cout<<((mat[(j+1)%3][(i+1)%3] * mat[(j+2)%3][(i+2)%3]) -
(mat[(j+1)%3][(i+2)%3] * mat[(j+2)%3][(i+1)%3]))/
determinant<<"\t";
j++;
}
cout<<"\n";
i++;
}
return 0;
}
Output
Friend, That was a nice question to answer
If you have any doubts in understanding do let me know in the
comment section. I will be happy to help you further.
Please like it if you think effort deserves like.
Thanks
- O X C:\Users\visha OneDrive Documents\C++\inverse.exe FEnter elements of matrix row wise: 3 0 2 2 0 -2 0 1 1 3 Given matrix is: © 2 120 -2 0 1 1 determinant: 10 Inverse of matrix is: -0.2 0.2 0 -0.2 0.3 1 -0.2 -0.3 0 ------------------------------- - Process exited after 95.67 seconds with return value o Press any key to continue ...