// Matrix Inverse.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int m,i,j,k,r,c,a,row,col,tru=0;
float B[100][100],A[100][100],D[100][100],E[100][100],F[100][10];
printf("Enter the number of rows or columns in the coefficient matrix: ");
scanf("%d",&m);
printf("Enter the coefficients in row-major order:\n");
//Taking the input coefficient matrix into the array A.
//Copying the array A into D for elementary tranformations.
//Copying the array A into E for verification.
//Initializing the elements of array B to make it an identity matrix.
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
scanf("%f",&A[i][j]);
D[i][j]=A[i][j];
E[i][j]=A[i][j];
if(i==j)
{
B[i][j]=1;
}
else
{
B[i][j]=0;
}
}
}
//Taking the constant terms into the array F.
printf("Enter the matrix of constant terms:\n");
for(j=0;j<1;j++)
{
for(i=0;i<m;i++)
{
scanf("%f",&F[i][j]);
}
}
//Printing the original matrix.
printf("The original matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%6.2f",A[i][j]);
}
printf("\n");
}
i=0;j=0;
//Logic for elementary transformations.
while(i!=m)
{
for(a=0;a<m;a++)
{
if(D[i][j]==0)
{
tru=1;
break;
}
B[i][a]=B[i][a]/D[i][j];//Applying the operation below on the identity matrix B.
A[i][a]=A[i][a]/D[i][j];//Converting the diagonal elements of A into 1.
}
if(tru==1)
break;
for(row=0;row<m;row++)
{
for(col=0;col<m;col++)
{
D[row][col]=A[row][col];//Copying the present state of A into D.
}
}
for(c=0;c<m;c++)
{
for(r=0;r<m;r++)
{
if(r!=i)
{
A[r][c]=A[r][c]-(D[r][j]*A[i][c]);//Converting the non-diagonal elements of same column of A to 0.
B[r][c]=B[r][c]-(D[r][j]*B[i][c]);//Applying the same operations to B.
}
}
}
for(row=0;row<m;row++)
{
for(col=0;col<m;col++)
{
D[row][col]=A[row][col];//Copying the present state of A into D.
}
}
i++;j++;
}
printf("\nThe original matrix after transformation:\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%6.2f",A[i][j]);
}
printf("\n");
}
printf("\nThe inverse of the matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%6.2f",B[i][j]);
}
printf("\n");
}
printf("\nVerification\n");
//Multiplying the obtained inverse matrix with a copy of the original matrix.
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
k=0;
A[i][j]=0;
D[i][j]=0;
while(k!=m)
{
A[i][j]=A[i][j]+E[i][k]*B[k][j];
k++;
}
}
}
//Printing the identity matrix obtained.
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%6.2f",A[i][j]);
}
printf("\n");
}
printf("\nThe answer matrix is:\n");
//Obtaining the answers by multiplying the inverse with the constant matrix.
for(i=0;i<1;i++)
{
for(j=0;j<m;j++)
{
k=0;
D[j][i]=0;
while(k!=m)
{
D[j][i]=D[j][i]+(B[j][k]*F[k][i]);
k++;
}
printf("%6.2f",D[j][i]);
}
printf("\n");
}
printf("\n");
if(tru==1)
{
printf("\nThe system of equations does not have a unique solution.\n");
}
return 0;
}
Enter the number of rows or columns in the coefficient matrix: 3
Enter the coefficients in row-major order:
4 3 -2
1 1 0
3 0 1
Enter the matrix of constant terms:
7 5 14
The original matrix is:
4.00 3.00 -2.00
1.00 1.00 0.00
3.00 0.00 1.00
The original matrix after transformation:
1.00 0.00 0.00
0.00 1.00 0.00
0.00 0.00 1.00
The inverse of the matrix is:
0.14 -0.43 0.29
-0.14 1.43 -0.29
-0.43 1.29 0.14
Verification
1.00 0.00 0.00
0.00 1.00 0.00
-0.00 -0.00 1.00
The answer matrix is:
2.86 2.14 5.43
Press any key to continue . . .