Question

In: Computer Science

What is the C programming code to find the inverse of a 2*2 matrix?

What is the C programming code to find the inverse of a 2*2 matrix?

Solutions

Expert Solution

output:

CODE:

#include<stdio.h>
#include<math.h>
/* function declarations*/

float determinantvalue(float [][25], float);
void cofactorvalue(float [][25], float);
void transposematrix(float [][25], float [][25], float);
int main()
{
float arr[25][25], k=2, d;
int i, j;
printf("Enter the elements of %.0fX%.0f Matrix : \n", k, k);
for (i = 0;i < k; i++)
{
for (j = 0;j < k; j++)
{
scanf("%f", &arr[i][j]);
}
}
/* calling determinant function for det value finding*/
d = determinantvalue(arr, k);
if (d == 0)
printf("\nInverse matrix is not possible");
else
cofactorvalue(arr, k);
}

/*function for determinant value*/
float determinantvalue(float arr[25][25], float k)
{
float s = 1, det = 0, b[25][25];
int i, j, m, n, c;
if (k == 1)
{
return (arr[0][0]);
}
else
{
det = 0;
for (c = 0; c < k; c++)
{
m = 0;
n = 0;
for (i = 0;i < k; i++)
{
for (j = 0 ;j < k; j++)
{
b[i][j] = 0;
if (i != 0 && j != c)
{
b[m][n] = arr[i][j];
if (n < (k - 2))
n++;
else
{
n = 0;
m++;
}
}
}
}
det = det + s * (arr[0][c] * determinantvalue(b, k - 1));
s = -1 * s;
}
}

return (det);
}

void cofactorvalue(float num[25][25], float f)
{
float b[25][25], fac[25][25];
int p, q, m, n, i, j;
for (q = 0;q < f; q++)
{
for (p = 0;p < f; p++)
{
m = 0;
n = 0;
for (i = 0;i < f; i++)
{
for (j = 0;j < f; j++)
{
if (i != q && j != p)
{
b[m][n] = num[i][j];
if (n < (f - 2))
n++;
else
{
n = 0;
m++;
}
}
}
}
fac[q][p] = pow(-1, q + p) * determinantvalue(b, f - 1);
}
}
transposematrix(num, fac, f);
}
/*to find transpose of matrix*/
void transposematrix(float num[25][25], float fac[25][25], float r)
{
int i, j;
float b[25][25], inversematrix[25][25], d;
for (i = 0;i < r; i++)
{
for (j = 0;j < r; j++)
{
b[i][j] = fac[j][i];
}
}
d = determinantvalue(num, r);
for (i = 0;i < r; i++)
{
for (j = 0;j < r; j++)
{
inversematrix[i][j] = b[i][j] / d;
}
}
printf("\nThe inverse of matrix is : \n");
for (i = 0;i < r; i++)
{
for (j = 0;j < r; j++)
{
printf("\t%f", inversematrix[i][j]);
}
printf("\n");
}
}


Related Solutions

wrote a code in mathlab to find the inverse of a matrix (5x5), USING CoFACTOR method....
wrote a code in mathlab to find the inverse of a matrix (5x5), USING CoFACTOR method. use numbers 1-15 as variables
Use this theorem to find the inverse of the given matrix or show that no inverse...
Use this theorem to find the inverse of the given matrix or show that no inverse exists. (If an answer does not exist, enter DNE in any cell.) 1    2    5    1 −1    0    2    1 2    1    −5    0 1    1    2    1
Find the inverse of the matrix [ 1 1 4 ] [ 3 2 4 ]...
Find the inverse of the matrix [ 1 1 4 ] [ 3 2 4 ] [ 1 1 6 ] It is a 3*3 matrix
Find the inverse of the matrix A= 2 -1 3 0 1 1 -1 -1 0
Find the inverse of the matrix A= 2 -1 3 0 1 1 -1 -1 0
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
what will be the code in C programming for the client and server chat application for...
what will be the code in C programming for the client and server chat application for the below issue :- write the C Programming code that able client have a unique ID to be known by the server
Use the matrix inverse and the matrix division method to solve the following set for x and y in terms of c:
Use the matrix inverse and the matrix division method to solve the following set for x and y in terms of c:
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT