C program: Gauss Jordon Method to find inverse of a Matrix


c-program-gauss-jordon-method-find-inverse-matrixc1First we have to enter the input matrix in our data structure. So I have asked for value of ‘n’ from user using scanf statement at line number 10 and create a corresponding matrix in memory using malloc statement at line number 12 . Since we have a square a matrix of size nXn, so we have to create a 2D array. For that a one dimensional array of pointers has been created and all of these pointers then contain address of 1D memory. This way we have stored nXn elements of matrix.

c-program-gauss-jordon-method-find-inverse-matrixc2Now we have allocated the required memory for inverse matrix which has to be stored in memory. And after this from line number 26-28 , we have taken the input matrix from user.c-program-gauss-jordon-method-find-inverse-matrixc3Now from line number 30-58, we have implemented the logic for computing inverse using Gauss Jordon method in C program. From line 30-35, we have initialized the inverse matrix. Diagonal elements are filled with value 1 and rest with value 0. Then a for loop is run on size of the input matrix and in inner for loop we have modified the input matrix and inverse matrix at each iteration by dividing each element by a particular element of matrix. Another inner for loop is there which also runs for size of matrix for each outer iteration and this modifies the input and inverse matrices too.


Finally we print the matrix which is already stored in Inverse array.
Here are some screenshots of program run->c-program-gauss-jordon-method-find-inverse-matrixc5

c-program-gauss-jordon-method-find-inverse-matrixc6Download source code C program: Gauss Jordon Method to find inverse of a Matrix