Search code examples
cmatrixlogicdeterminants

Getting N x N matrix determinant in C


I've been trying to compute an N x N matrix determinant, so that this is what I've got so far. The result of if it's -378029970. I don't know what is going on because if I change the N value for the matrix size it works fine and prints the right result which is -20. I debugged the whole script and found out the problem is when I change the N value. I'm kind new in C language so I appreciate if you could give me a hand with it. Thanks!

void getCofactor(int q, int n, int matrix[][n], int temp[][n]) {
    int i = 0;
    int j = 0;
    int p = 0;

    for (int row = 0; row < n; row++) {
        for (int col = 0; col < n; col++) {
            if (row != p && col != q) {
                temp[i][j++] = matrix[row][col];
                if (j == n - 1) {
                    j = 0;
                    i++;
                }
            }
        }
    }
}

int determinante(int n, int matrix[][n]) {
    int D = 0;

    if (n == 1) {
        return matrix[0][0];
    }

    int temp[n][n];
    int sign = 1;

    for (int f = 0; f < n; f++) {
        getCofactor(f, n, matrix, temp);

        D += sign * matrix[0][f] * determinante(n - 1, temp);

        sign = -sign;
    }
    return D;
}

int main() {

    int matrix[3][3] = { { 3, -2, 5}, { -2, 8, 10}, { 3, -2, 4 }};
    int LINHA = 3;

    printf("Determinante: %d", determinante(LINHA, matrix));

    return 0;
}

Solution

  • The problem is temp is defined as int temp[n][n] but this matrix' dimensions are incompatible when passed to determinante(n - 1, temp).

    You should modify the definition of temp to reduce the size and modify the prototype of getCofactor to reflect the actual dimensions of matrix and temp.

    Here is a modified version:

    #include <stdio.h>
    #include <stdlib.h>
    
    void getCofactor(int q, int n, int matrix[][n], int temp[][n - 1]) {
        int i = 0;
        int j = 0;
        int p = 0;
    
        for (int row = 0; row < n; row++) {
            for (int col = 0; col < n; col++) {
                if (row != p && col != q) {
                    temp[i][j++] = matrix[row][col];
                    if (j == n - 1) {
                        j = 0;
                        i++;
                    }
                }
            }
        }
    }
    
    int determinante(int n, int matrix[][n]) {
        if (n == 1) {
            return matrix[0][0];
        }
    
        int temp[n - 1][n - 1];
        int sign = 1;
        int D = 0;
    
        for (int f = 0; f < n; f++) {
            getCofactor(f, n, matrix, temp);
            D += sign * matrix[0][f] * determinante(n - 1, temp);
            sign = -sign;
        }
        return D;
    }
    
    int main() {
        int matrix[3][3] = { { 3, -2, 5}, { -2, 8, 10}, { 3, -2, 4 } };
        int LINHA = sizeof(matrix) / sizeof(matrix[0]);
    
        printf("Determinante: %d\n", determinante(LINHA, matrix));
        return 0;
    }