Matrix inversion in C++

Hi there, I'm trying to invert matrices up to 12 x 12 on an mbed MCU using C++. The program works for a 3x3 matrix, but not a higher level one and uses Gaussian elimination. Am I missing an obvious syntax error that would effect higher level matrices?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int main()
{

int a[4][4]={1,2,3,4,5,6,0,0,1,2,4,5,0,1,2,3};
int b[4][4]={1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1};
int i,j,k,l;
double t;

for (j = 0; j<4; j++){
    for (i =0; i<4; i++){
        if (i==j){
        if (a[i][j] != 0){
            t = 1/a[j][j];
            for (k = 0; k<4; k++){
                a[j][k] *= t;
                b[j][k] *= t;
            }
            for (l = 0; l<4; l++){
                if (l != j){
                    t = -a[l][j];
                    for (k = 0; k<4; k++){
                        a[l][k] = a[l][k] + t * a[j][k];
                        b[l][k] = b[l][k] + t * b[j][k];
                    }
                }
           }
           }           
        }
    }
}

}
Integer division.

Another thing:
1
2
3
4
5
    for (i =0; i<4; i++){
        if (i==j){
           //...
        }
    }
¿why are you looping then?
Last edited on
Topic archived. No new replies allowed.