# C program - inverse of a matrix

• 11-06-2008
chaugh
C program - inverse of a matrix
hi, this is my first time on this board, so i apologise in advanced if i broke any rules or whatever...
so basically, im supposed to use C to find theinverse of a matrix of whose size and values are entered by the user. I am supposed to do so using the following steps:

Step 1: Set up an n × n matrix B and initialize it to the n × n identity matrix.

Step 2: In the matrix A, interchange the top row with the nearest row below it, if necessary,
to bring a nonzero entry to the row 1, column 1 position. (This is a Row Interchange
elementary row operation.) Apply the same Row Interchange elementary
row operation to the matrix B.

Step 3: In the matrix A, if the entry in the row 1, column 1 position is a, multiply the first
row by 1/a in order to make the entry in the row 1, column 1 position have the value
1. (This is a Row Scaling elementary row operation.) Apply the same Row Scaling
elementary row operation to the matrix B.

Step 4: In the matrix A, add suitable multiples of the top row to the rows below so that
all entries below the 1 in the row 1, column 1 position become zero. (These are Row
Addition elementary row operations.) Apply the same Row Addition elementary
row operations to the matrix B.

Step 5: Repeat Steps 2 through 4 to make each diagonal entry Ai,i have the value 1, for
i = 2, 3, 4, . . . , n, and each off-diagonal entryAi,j have the value 0, i, j = 2, 3, 4, . . . , n,
i 6= j. In Step 2, if it is necessary to interchange rows to make element Ai,i nonzero,
interchanging row i with the nearest row below it having Aj,i nonzero. In Step 4,
when zeroing elements in column i, add suitable multiples of row Ri to rows Rj , for
j = 1, . . . , i − 1, i + 1, . . . , n (that is, both above and below the 1 in element Ai,i).

and here is what i have done so far ( sorry i have to post the entire code)
Code:

```#include <stdio.h> #include <stdlib.h> int main () {                void printMatrix (char name, double **matrix, int n);                 int input, entries, i, j;         double **A, **B, temp;         printf("Enter dimension of square matrix:");         scanf ("%d", &input);         entries = input * input;         printf ("Enter the %d entries of the matrix:", entries);                 A = (double **) malloc (input* sizeof (double *));         for (i=0; i<input; i++)         {                 A[i] = (double *) malloc (input* sizeof (double));         }                 B = (double **)malloc (input* sizeof (double *));         for (j=0; i<input; i++)         {                 B[i] = (double *) malloc (input* sizeof (double));         }         for (i=0; i<input; i++)         {                 for (j=0; j<input; j++)                 {                         scanf("%lf", &A[i][j]);                 }         }                 for (i=0; i<input; i++)         {                 for (j=0; j<input; j++)                 {                         if ( i == j)                                 B[i][j] = 1;                         else                                 B[i][j] = 0;                 }         }                 if (A[0][0]==0)                 {                 for(i=0;i<input;i++)                         {                                 temp=A[0][i];                                 A[0][i]=A[1][i];                                 A[1][i]=temp;                                                 temp=B[0][i];                                 B[0][i]=B[1][i];                                 B[1][i]=temp;                         }                 }         temp = A[0][0];                 for(i=0;i<input;i++)         {                 for(j=0;j<input;j++)                 {                         A[i][j]/=temp;                         B[i][j]/=temp;                 }         }         for (i=1;i<input;i++)         {                 temp = A[i][0];                 for(j=0;j<input;j++)                 {                         A[i][j]-=A[0][j]*temp;                         B[i][j]-=B[0][j]*temp;                 }         }                                                 for (i=0; i<input; i++)         {                 printf ("\n");                 for (j=0; j<input; j++)                 {                         printf("%f ", A[i][j]);                 }         }                         for (i=0; i<input; i++)         {                 printf ("\n");                 for (j=0; j<input; j++)                 {                         printf("%f ", B[i][j]);                 }         } return 0;        }```
As you can see, i am up to step 5. I have 2 problems - first, for some really odd reason it only works with matrix of size less then 2x2. anything larger it will just ask for input then the program will end. i suspect there is something wrong with the nested for loops of inputting 0s and 1s into B to make it the identity. however, havin said that i cant find anything wrong with anything.
secondly, i am unsure how to proceed with step 5, any help is appreciated.
i am half asleep when im typing this, so ignore and any grammar or spelling or if i havnt explained myself properly.
• 11-06-2008
nucleon
This may not be the only problem, but look closely at this part:
Code:

```    for (j=0; i<input; i++)     {         B[i] = (double *) malloc (input* sizeof (double));     }```
• 11-07-2008
chaugh
wow thx!! that solved the problem straight away!
• 11-05-2009