INVERSE OF A MATRIX
This post is for CLASS 12 Students. Are you bored to find Determinant, Cofactor, Adjoint and Finally Inverse ?
What if you type the question and all the above things come directly on your computer screen.
First I will say how did i do this?
First I will say how did i do this?
- Know the normal way
- Find the shortcut. Here I used a Shortcut.
- Write down Algorithm.
- Now Start Coding.
- You should be focussed on your output.
- Combine Maths and C++
- That's it. You did it.
Here is a sample.
Or what if Determinant is ZERO.Check this
What NEXT?..........CODE. Copy and paste it in your Notepad and change it into .cpp. Here is the code.
- #include<iostream.h>
- #include<conio.h>
- void get(int a[3][3])
- {
- for(int i=0;i<3;i++)
- {
- for(int j=0;j<3;j++)
- {
- cout<<"Enter the value of...a["<<i+1<<"]["<<j+1<<"]....";
- cin>>a[i][j];
- }
- }
- }
- void disp(int d[3][3])
- {
- cout<<"\n";
- for(int i=0;i<3;i++)
- {
- for(int j=0;j<3;j++)
- {
- cout<<"\t"<<d[i][j];
- }
- cout<<"\n";
- }
- }
- int det(int a[3][3])
- {
- int mod;
- mod=(a[0][0]*(a[1][1]*a[2][2]-a[2][1]*a[1][2]))-(a[0][1]*(a[1][0]*a[2][2]-a[2][0]*a[1][2]))+(a[0][2]*(a[1][0]*a[2][1]-a[2][0]*a[1][1]));
- cout<<"\nDeterminant of A="<<mod;
- return mod;
- }
- void main()
- {
- clrscr();
- int a[3][3],mod,adj[3][3],co[3][3];
- get(a);
- disp(a);
- mod=det(a);
- cout<<"\nA Inverse is (1/Determinant of A)*Adjoint A";
- if(mod==0)
- {
- cout<<"\nInverse doesnt exist.";
- }
- else
- {
- co[0][0]=a[1][1]*a[2][2]-a[2][1]*a[1][2];
- co[0][1]=a[1][2]*a[2][0]-a[2][2]*a[1][0];
- co[0][2]=a[1][0]*a[2][1]-a[2][0]*a[1][1];
- co[1][0]=a[2][1]*a[0][2]-a[0][1]*a[2][2];
- co[1][1]=a[2][2]*a[0][0]-a[0][2]*a[2][0];
- co[1][2]=a[2][0]*a[0][1]-a[0][0]*a[2][1];
- co[2][0]=a[0][1]*a[1][2]-a[1][1]*a[0][2];
- co[2][1]=a[0][2]*a[1][0]-a[1][2]*a[0][0];
- co[2][2]=a[0][0]*a[1][1]-a[1][0]*a[0][1];
- cout<<"\nCofactor matrix...";
- disp(co);
- adj[0][0]=a[1][1]*a[2][2]-a[2][1]*a[1][2];
- adj[1][0]=a[1][2]*a[2][0]-a[2][2]*a[1][0];
- adj[2][0]=a[1][0]*a[2][1]-a[2][0]*a[1][1];
- adj[0][1]=a[2][1]*a[0][2]-a[0][1]*a[2][2];
- adj[1][1]=a[2][2]*a[0][0]-a[0][2]*a[2][0];
- adj[2][1]=a[2][0]*a[0][1]-a[0][0]*a[2][1];
- adj[0][2]=a[0][1]*a[1][2]-a[1][1]*a[0][2];
- adj[1][2]=a[0][2]*a[1][0]-a[1][2]*a[0][0];
- adj[2][2]=a[0][0]*a[1][1]-a[1][0]*a[0][1];
- cout<<"\nAdjoint matrix...";
- disp(adj);
- }
- getch();
- }
Now its your turn.Try making a code for 2x2 matrix.If you succeed comment below.
YOU can ask any other program like this in comments or you can mail me.