Search code examples
matrixdeterminants

calculate determinant of matrix


i am surprised why following algorithm shows me ,that matrix is not invertible,i have input identity matrix,here is my code for calculate teterminant

float determinant(float a[5][5],float k)
{
float s=1,det=0,b[5][5];
    int i,j,m,n,c;
    if(k==1){
        return (a[0][0]);
    }
    else
    {
        det=0;
        for(c=0;c<k;c++)

            m=0;
            n=0;
            for(i=0;i<k;i++)
            {
                for(j=0;j<k;j++){
                    b[i][j]=0;
                    if(i!=0 && j!=c)
                    {
                        b[m][n]=a[i][j];
                        if(n<(k-2))
                            n=n+1;

                        else{
n=0;
                        m++;

                    }



        }
    }

            }
            det=det+s*(a[0][c])*determinant(b,k-1);
            s=-1*s;
        }


   return det;
}

Solution

  • While trying to fix your horrible formatting I realized you're probably missing braces for the first for loop (that with the c). Protip: format your code properly and you don't make such simple errors.