Saturday 17 August 2019

C program for inverse of a matrix


/*Inverse of a matrix*/
#include<stdio.h>
#include<math.h>
void
main()
{
                int i,j,det,k,a[20][20];
                float I[20][20],U[20][20];
                printf("enter the elements of input matrix:");
                for(i=0;i<2;i++)
                {
                                for(j=0;j<2;j++)
                                                scanf("%d",&a[i][j]);
                }
                printf("the input matrix is:");
                printf("\n");
                for(i=0;i<2;i++)
                {
                                for(j=0;j<2;j++)
                                                printf("%d\t",a[i][j]);
                                printf("\n");
                }
                /*matrix determinant*/
                det=(a[0][0]*a[1][1])-(a[0][1]*a[1][0]);
                printf("\n The determinant is=%d\n",det);
                /*matrix inverse*/
                if(det==0)
                                printf("The matrix inverse is not possible:");
                else
                {
                                for(i=0;i<2;i++)
                                {
                                                for(j=0;j<2;j++)
                                                {
                                                                if(i==j)
                                                                {
                                                                                I[i][j]=(a[(i+1)%2][(j+1)%2]);
                                                                }
                                                                else
                                                                {
                                                                                I[i][j]=-(a[i][j]);
                                                                }
                                                }
                                }
                                printf("The inverse matrix is \n");
                                for(i=0;i<2;i++)
                                {
                                                for(j=0;j<2;j++)
                                                                printf("%.4f\t",(I[i][j]/det));
                                                printf("\n");
                                }
                                /*product matrix*/
                                for(i=0;i<2;i++)
                                {for(j=0;j<2;j++)
                               
                                {
                                                U[i][j]=0;
                                                for(k=0;k<2;k++)
                                                                U[i][j]=U[i][j]+a[i][k]*I[k][j];
                                }
                                }
                                printf("\n the product matrix is:\n");
                                for(i=0;i<2;i++)
                                {
                                                for(j=0;j<2;j++)
                                                                printf("%.4f\t",(U[i][j]/det));
                                                printf("\n");
                                }
                }
}
RESULT 1:
enter the elements of input matrix:1 2 3 4
the input matrix is:
1       2
3       4
 The determinant is=-2
The inverse matrix is
-2.0000 1.0000
1.5000  -0.5000
 the product matrix is:
1.0000  0.0000
0.0000  1.0000
Press any key to continue
RESULT 2:
enter the elements of input matrix:1 1 1 1
the input matrix is:
1       1
1       1
 The determinant is=0
The matrix inverse is not possible:
Press any key to continue

No comments:

Post a Comment

Thanks for comment.