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.

Edited 5 Years Ago by Dasharnb777: n/a

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.

Edited 5 Years Ago by Momerath: n/a

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 ?

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

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?

Comments
Guidance of a real pro!

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!

This article has been dead for over six months. Start a new discussion instead.