Hi, 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; j++)
                        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.

Dani AI

Generated

As pointed out and as later confirmed, the original crash was a classic index/loop problem combined with a bad input. Beyond fixing loop indices, add defensive checks up front (null, square matrix, and that the supplied n matches the array size) and detect zero pivots before dividing. That prevents obscure IndexOutOfRangeException and makes failures explicit.

A robust in-place elimination with partial pivoting (copies the input so the caller's array is not modified, swaps rows while tracking sign, returns 0 for singular matrices) looks like this:

public static double Determinant(double[,] input)
{
    if (input == null) throw new ArgumentNullException(nameof(input));
    int n = input.GetLength(0);
    if (n != input.GetLength(1)) throw new ArgumentException("Matrix must be square");
    double[,] a = (double[,])input.Clone();
    int sign = 1;
    for (int i = 0; i < n; i++)
    {
        int pivot = i;
        double max = Math.Abs(a[i,i]);
        for (int r = i+1; r < n; r++)
        {
            double v = Math.Abs(a[r,i]);
            if (v > max) { max = v; pivot = r; }
        }
        if (max == 0.0) return 0.0; // singular
        if (pivot != i) { for (int c = 0; c < n; c++) { var t = a[i,c]; a[i,c] = a[pivot,c]; a[pivot,c] = t; } sign = -sign; }
        for (int r = i+1; r < n; r++)
        {
            double f = a[r,i] / a[i,i];
            for (int c = i; c < n; c++) a[r,c] -= f * a[i,c];
        }
    }
    double det = sign;
    for (int i = 0; i < n; i++) det *= a[i,i];
    return det;
}

Notes: every row swap flips the determinant sign. Partial pivoting improves numerical stability; large matrices will still suffer floating-point error. For production code, a tested linear-algebra library is recommended. For debugging, log a.GetLength(0), a.GetLength(1) and loop indices just before the failing assignment to reveal mismatches quickly.

Recommended Answers

All 8 Replies

Line 10 you increment j instead of k. You'll also probably have an issue with line 7 since you allow j to go to n, which is outside the array bounds.

Line 10 you increment j instead of k. You'll also probably have an issue with line 7 since you allow j to go to n, which is outside the array bounds.

Oh, exactly! Thanx, but there's still ecxeption.

Did you change line 7 to j < n rather than j < n + 1 ?

Did you change line 7 to j < n rather than j < n + 1 ?

Oh, thanx. But it doesn't help...

Exception still in the same line? Could you post the current version of your code?

Exception still in the same line? Could you post the current version of your code?

Yes, exception is always in this line.
Current 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; 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
                }
            }
            det = 1;
            for (i = 0; i < n; i++)
                det = det * a[i, i];
                return det;
        }

I've tried to get it to generate an exception in that line and can't do it. What input are you using?

commented: Guidance of a real pro! +8

I've tried to get it to generate an exception in that line and can't do it. What input are you using?

Oh, thanx a lot! I'm so so inconsiderate! I'd wrong input. It works now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.