Error in programme C (determinant calculation)

Asked

Viewed 135 times

2

#include <stdio.h>
#include <math.h>

void zerar(int n,int m[][n]) {
    int i, j;
    for (i=0; i<n; i++) {
        for (j=0; j<n; j++) {
            m[i][j] = 0;
        }
    }
}

void printm(int n, int matriz[][n]) {
    int i,j;
    for (i=0; i<n; i++) {
        for (j=0; j<n; j++) {
            printf("\t%d", matriz[i][j]);
        }
        printf("\n");
    }
}

int det(int n, int matriz[][n]) {
    int i, j, k, x, y, soma=0, aux[10][10];
    zerar(n, aux);
    if (n < 1) {}
    else if (n == 1) {
        return matriz[0][0];
    }
    else if (n == 2) {
        soma = (matriz[0][0] * matriz[1][1]) - (matriz[0][1] * matriz[1][0]);
        return soma;
    }
    else {
        for (i=0; i<n; i++) {
            for (j=1, x=0; j<n; j++) {
                for (k=0, y=0; k<n; k++) {
                    if (k == i) {
                        continue;
                    }
                    else {
                        printf("\n\n");
                        printm(n-1, aux);
                        aux[x][y] = matriz[j][k];
                        printf("\nx=%d, y=%d, j=%d, k=%d, i=%d\n", x, y, j, k, i);
                        y++;
                    }
                }
                x++;
            }
            soma += matriz[0][i]*pow(-1, i+2)*det(n-1, aux);
        }
        return soma;
    }
}


int main()
{
    int m[3][3] = {{4, 3, 2}, {1, 4, 5}, {2, 1, 2}};
    det(3, m);
    printf("%d", det(3, m));
    printf("\n\n");
    printm(3, m);
    printf("\n\n");
}

http://pastebin.com/6iGeaxHD#

In the "Else" of line 41 I put a function to print the auxiliary matrix and a printf to display the values of the counters at each step, doing this I found the problem: no values are being saved in the 2nd row of the auxiliary matrix in any of the cases, keeping the value 0 (value with which these elements were initialized). How can I correct??

Example: in the first function for loop det, when it rotates with the value i=0, the following auxiliary matrix is printed after the entire display:

4 5
0 0

when it should be:

4 5
1 2

1 answer

4

Your problem is how you are creating the array aux:

int i, j, k, x, y, soma=0, aux[10][10];

You are creating it with size 10x10, which means that internally a sequence of 100 elements is created. So when you assign:

aux[1][0] = 1;

It is the 11th element that is being assigned. Your array then ends like this:

4 5 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0...

When you try to pass this array to function det (or even printm), you establish that the size of the matrix is n - in the case 2:

int det(int n, int matriz[][n]) {

Then it takes only the first 4 elements of the array!

4 5 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0...
^^^^^^^

To solve this, create your variable aux with the correct size you want to use:

int i, j, k, x, y, soma=0, aux[n-1][n-1];

Example working on ideone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.