Nishit
Nishit

Reputation: 1

What is the error in this computation of matrix determinant?

I'm trying to find determinant of 4x4 matrix, It gets compiled, takes the values for matrix and then suddenly terminates showing some unknown error caused it to stop working.

#include <stdio.h>

int detcalfnl(int a,int b,int c,int d)
{
    int anss;
    anss = ((a*d)-(b*c));
    return anss;
}
/*********************************************************************************************************************************************************/
void detcalthree(int mat[3][3],int *ansss)
{
   *ansss=0;
   int x;
   for(x=0;x<3;x++)
       {
           if(x==0)
           {
            *ansss+=((mat[0][x])*(detcalfnl(mat[x+1][x+1],mat[x+1][x+2],mat[x+2][x+1],mat[x+2][x+2])));
           }
           else if(x==1)
           {
            *ansss+=((-1)*((mat[0][x])*(detcalfnl(mat[x][x-1],mat[x][x+1],mat[x+1][x-1],mat[x+1][x+1]))));
           }
           else if(x==2)
           {
            *ansss+=((mat[0][x])*(detcalfnl(mat[x-1][x-2],mat[x-1][x-1],mat[x][x-2],mat[x][x-1])));
           }
       }
   }
/*********************************************************************************************************************************************************/
void detcalfour(int mat[4][4],int *ansss)
{
   *ansss=0;
   int a1[3][3],a2[3][3],a3[3][3],a4[3][3];
   int x,row,clm,a5,a6,a7,a8;
   int *p5=&a5;
   int *p6=&a6;
   int *p7=&a7;
   int *p8=&a8;

    for(x=0;x<4;x++)
    {
        int a=0;
        int b=0;
        for(row=1;row<4;row++)
        {
            for(clm=0;clm<4;clm++)
            {
                if(clm==x){continue;}
                else
                    {
                if(x==0)
                {
                    a1[b][a]=mat[row][clm];
                }
                else if(x==1)
                {
                    a2[b][a]=mat[row][clm];
                }
                else if(x==2)
                {
                    a3[b][a]=mat[row][clm];
                }
                else if(x==3)
                {
                    a4[b][a]=mat[row][clm];
                }
                }
                a++;
            }
            b++;
        }
    }
    detcalthree(a1,p5);
    detcalthree(a2,p6);
    detcalthree(a3,p7);
    detcalthree(a4,p8);
    *ansss=((mat[0][0])*(*p5))-((mat[0][1])*(*p6))+((mat[0][2])*(*p7))-((mat[0][3])*(*p8));
   }
/*********************************************************************************************************************************************************/
int main()
{
     int dim,row,clm,fnlans;

    printf("Please Enter The Dimension Of Matrix :");
    scanf("%d",&dim);
    printf("\n\n\n");
     int oprnd[dim][dim];

    for(row=0;row<dim;row++)
    {
        for(clm=0;clm<dim;clm++)
        {
            printf("\nPlease Provide Element For Row %d and Column %d : ",row+1,clm+1);
            scanf("%d",&oprnd[row][clm]);
        }
    }
 int *pfs=&fnlans;
 detcalfour(oprnd,pfs);
    printf("\n\n\nValue Of Determinant Is %d \n\n",*pfs);
     return 0;
}

Upvotes: 0

Views: 53

Answers (1)

Eric Postpischil
Eric Postpischil

Reputation: 222650

In detcal4, inside the loop on x, a and b are set to zero and a is incremented inside the inner loop (on clm) using a++. This incrementing is repeated even as the outer loop iterates; a is never reset to zero. So it increases beyond the array dimensions. This results in the code accessing the arrays a1, a2, a3, and a4 out of bounds, corrupting memory.

Reset a to zero each time the loop on clm is started. Move the int a=0; from outside the loop on row to inside it.

As a matter of good general practice, declare variables just where they are needed. Since a is not needed outside the loop on row, it should not be declared outside that loop, and declaring it inside automatically avoids this error.

Upvotes: 1

Related Questions