Ir para conteúdo
Fórum Script Brasil

Jonnathann Finizola

Membros
  • Total de itens

    1
  • Registro em

  • Última visita

Posts postados por Jonnathann Finizola

  1. Estou tentando fazer um programa que calcula o determinante de matrizes de qualquer ordem usando cofatores. Para isso estou usando alocação dinamica em C. Implementei duas funções em C chamadas cofator e det_matrix ambas chamam umas as outras. O código compila numa boa sem apresentar erros ou warnings, porém quando executo o codeblocks apresenta um erro dizendo que o programa precisa ser fechado. Eis o código abaixo:

    ps: alguém poderia me ajudar?

     

    double det_matrix(Matrix* matrix, int order)
    {
    double cofator(Matrix* matrix, int order, int i_matrix, int j_matrix);

    double det = 0.0;
    int j;
    
    if(matrix->rows == 1)
    {
        det = matrix->point[0][0];
    }
    else
    {
        for(j = 0; j < order; j++)
        {
            det = det + (matrix->point[0][j] * cofator(matrix, matrix->rows, 0, j));
        }
    }
    return det;

    }

    double cofator(Matrix* matrix, int order, int i_matrix, int j_matrix)
    {

    double **sub_matrix = malloc_matrix(matrix->rows, matrix->cols);
    
    int i, j;
    int x = 0;
    int y = 0;
    
    int n = order - 1;
    
    for(i = 0; i < order; i++)
    {
        for(j = 0; j < order; j++)
        {
    
            if(i != i_matrix && j != j_matrix)
            {
    
                sub_matrix[x][y] = matrix->point[i][j];
                y++;
    
                //printf("%i %i", x, y);
                if(y >= n)
                {
                    x++;
                    y = 0;
                }
            }
        }
    }
    Matrix *returned = (Matrix*)malloc(sizeof(Matrix));
    returned->rows = matrix->rows;
    returned->cols = matrix->cols;
    returned->point = sub_matrix;
    
    return pow(-1, i_matrix + j_matrix) * det_matrix(returned, matrix->rows);

    }

×
×
  • Criar Novo...