// Find the inverse of a 2x2 matrix using the following method. Swap the // diagonals. Change the sign of the non-diagonal elements. Find the determinant // of this new matrix. Multiply each term of the new matrix by the inverse of the // determinant. This is the inverse of the matrix. Check that this is the inverse // by multiplying with itself // // Author:rahule@cse.iitk.ac.in #include main() { float a[2][2],det,a_inv[2][2],I[2][2]; printf("enter the 2x2 matrix\n"); scanf("%f%f%f%f",&a[0][0],&a[0][1],&a[1][0],&a[1][1]); //reading 2x2 matrix det=a[0][0]*a[1][1]-a[0][1]*a[1][0]; //calculating the determinant printf("\n the determinant is %f\n",det); //finding the inverse a_inv[0][0]=a[1][1]; //swapping the diagonals a_inv[1][1]=a[0][0]; a_inv[0][1]=(-1)*a[0][1]; //changing the sign of non-diagonal elements a_inv[1][0]=(-1)*a[1][0]; printf("\nafter swapping and changing the diagonals the matrix becomes :\n"); printf("%f %f\n%f %f\n",a_inv[0][0],a_inv[0][1],a_inv[1][0],a_inv[1][1]); if(det==0) //if determinant is zero,inverse can not be found { printf("\nthe determinant of the matrix is zero.cannot find the inverse!exiting.."); } else { a_inv[0][0]=a_inv[0][0]/det; //calculating inverse a_inv[0][1]=a_inv[0][1]/det; a_inv[1][0]=a_inv[1][0]/det; a_inv[1][1]=a_inv[1][1]/det; printf("\nthe inverse of the matrix is :\n"); //prints the inverse printf("%f %f\n%f %f\n",a_inv[0][0],a_inv[0][1],a_inv[1][0],a_inv[1][1]); printf("\nmultiplying the matrix with its inverse,we get :\n"); //calculating the value of a*a_inverse I[0][0]=a[0][0]*a_inv[0][0]+a[0][1]*a_inv[1][0]; I[0][1]=a[0][0]*a_inv[0][1]+a[0][1]*a_inv[1][1]; I[1][0]=a[1][0]*a_inv[0][0]+a[1][1]*a_inv[1][0]; I[1][1]=a[1][0]*a_inv[0][1]+a[1][1]*a_inv[1][1]; printf("%f %f\n%f %f\n",I[0][0],I[0][1],I[1][0],I[1][1]); } } //note that multiplying a matrix with its inverse always results in an identitymatrix