Search code examples
c#matrixdeterminants

How to calculate matrix determinant? n*n or just 5*5


everyone. I need to find matrix n*n (or 5*5) determinant. I have a function translated from Pascal, but there's INDEX OUT OF RANGE EXCEPTION. Could somebody help me?

Here's my code:

public static double DET(double[,] a, int n)
    {
        int i, j, k;
        double det = 0;
        for (i = 0; i < n - 1; i++)
        {   
            for (j = i + 1; j < n + 1; j++)
            {
                det = a[j, i] / a[i, i];
                for (k = i; k < n; k++)
                    a[j, k] = a[j, k] - det * a[i, k]; // Here's exception
            }
        }
        det = 1;
        for (i = 0; i < n; i++)
            det = det * a[i, i];
            return det;
    }

Thanx for any help.


Solution

  • for (j = i + 1; j < n + 1; j++)
    

    Last J value will be bigger than array size. So you must to recheck array sizes and all how was all indexes translated from pascal.