簡體   English   中英

矩陣分段故障

[英]Matrix Segmentation Fault

我正在嘗試編寫一個計算nXn矩陣行列式的代碼。 對於3x3矩陣,代碼運行良好,但在4X4矩陣或更大的矩陣上程序崩潰。 你能告訴我我做錯了什么嗎?

#include<stdio.h>
#include<stdlib.h>

double sum(int n, double **matrix);

int main(){

    double row, col, size;

    int i,j,k;

    FILE *fd=fopen("input10.txt","r");

    fscanf(fd,"%lf", &size);

    row = size;
    col = size;

        double ** matrix=(double **) malloc (row*sizeof (double*));

        if(matrix != NULL){
            for(k=0; k<row ;k++){
                matrix[k]=(double*) calloc (col,sizeof (double));
        }
    }


    for(i=0; i<row; i++){
        for(j=0; j<col; j++){
            fscanf(fd,"%lf",&matrix[i][j]);

        }
    }
    printf("%.2le",sum(size,matrix));  

    return 0;
}

double computeDeterminant(unsigned char end, unsigned char start, double **matrix)
{
    int i,j,k;
    double s=0;

    double row, col;
    row = end;
    col = end;
    double ** b=(double **) malloc (row*sizeof (double*));

        if(matrix != NULL){
            for(k=0; k<row ;k++){
                b[k]=(double*) calloc (col,sizeof (double));
        }
    }


    for(i=0;i<end;i++){
        for(j=0;j<end;j++){     
            b[i][j]=matrix[i][j];
        }
    }

    for(i=0;i<end-1;i++){   
        for(j=1;j<=end;j++){
            b[i][j]=b[i+1][j];
        }
    }


    for(i=0;i<end-1;i++){
        for(j=start;j<=end-1;j++){
            b[i][j]=b[i][j+1];
        }
    }


    if (end-1==2){
        return b[1][1]*b[2][2]-b[1][2]*b[2][1];
    }
    else{
        for (j=0;j<end-1;j++){
            s=s+pw(1+j)*b[1][j]*computeDeterminant(end-1,j,b);
        }return s;
    }
}

double sum(int n,double **matrix)
{
    int j;
    double s=0;

    if(n>2)
    {
        for(j=1;j<=n;j++)
            s=s+pw(1+j)*matrix[1][j]*computeDeterminant(n, j, matrix);
        return s;
    }
    else
        return matrix[1][1]*matrix[2][2]-matrix[1][2]*matrix[2][1];
}

int pw(int y)
{
     return (y%2)==0?1:-1; 
}

你分配了col=end; b[k]=(double*) calloc (col,sizeof (double)); col=end; b[k]=(double*) calloc (col,sizeof (double)); 但你在循環中for(j=start;j<=end-1;j++){ b[i][j]=b[i][j+1]; } b[i][end] for(j=start;j<=end-1;j++){ b[i][j]=b[i][j+1]; } for(j=start;j<=end-1;j++){ b[i][j]=b[i][j+1]; }函數。 computeDeterminant 你設置col=end+1; 或者你改變<= by <

if(matrix == NULL)
    return 0.0;

row = end;
col = end;
double ** b=(double **) malloc (row*sizeof (double*));
for(k=0; k<row ;k++){
    b[k]=(double*) calloc (col,sizeof (double));
}

for(i=0;i<end;i++){
    for(j=0;j<end;j++){    
        b[i][j]=matrix[i][j];
    }
}

for(i=0;i<end-1;i++){ // i<end-1 bcause of i+1  
    for(j=1;j<end;j++){ // j<end
       b[i][j]=b[i+1][j];
    }
}

for(i=0;i<end;i++){ // i<end
    for(j=start;j<end-1;j++){ // j<end-1 bcause of j+1
        b[i][j]=b[i][j+1];
    }
}

此外,您應該釋放在函數computeDeterminantb分配的內存

if (end-1==2){
    s = b[1][1]*b[2][2]-b[1][2]*b[2][1];
}
else{
    for (j=0;j<end-1;j++){
        s=s+pw(1+j)*b[1][j]*computeDeterminant(end-1,j,b);
    }
}

for(k=0; k<row ;k++){
    free( b[k] );
}
free(b);

return s;

在函數sum存在類似的問題:

double sum(int n,double **matrix)
{
    int j;
    double s=0;

    if(n>2)
    {
        // for(j=1;j<=n;j++) <- change this
        for(j=1;j<n;j++)
               //^
            s=s+pw(1+j)*matrix[1][j]*computeDeterminant(n, j, matrix);
        return s;
    }
    else
        return matrix[1][1]*matrix[2][2]-matrix[1][2]*matrix[2][1];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM