-1

I'm able to find det of matrix 2x2 using this code:

using System;

class find_det
{
static void Main()
{
    int[,] x = { { 3, 5, }, { 5, 6 }, { 7, 8 } };
    int det_of_x = x[0, 0] * x[1, 0] * x[0, 1] * x[1, 1];
    Console.WriteLine(det_of_x);
    Console.ReadLine();
}
}

But when I tried to find det of 3x3 Matrix, using this code:

using System;

class matrix3x3
{
    static void Main()
{
    int[,,] x={{3,4,5},{3,5,6},{5,4,3}};
    int det_of_x=x[0,0]*x[0,1]*x[0,2]*x[1,0]*x[1,1]*x[1,2]*x[2,0]*x[2,1]*x[2,2];
    Console.WriteLine(det_of_x);
    Console.ReadLine();
    }
}

It got error. Why?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user2078395
  • 11
  • 1
  • 1
  • Are you sure you calculate determinants? – qben Feb 16 '13 at 13:27
  • 2
    Horribly vague question. The error from your compiler should've given you atleast a hint of what is wrong here. But as the other's have said, it's still a 2D Array, not a 3D, remove the extra , in the type. – Eric Johansson Feb 16 '13 at 13:27
  • see http://stackoverflow.com/questions/5051528/how-to-calculate-matrix-determinant-nn-or-just-55 – Rachel Gallen Feb 16 '13 at 13:28
  • I don't know what you are calculating but it's also not the determinant of a matrix. The determinant of a 2x2 matrix A is det(A) = a11*a22 - a12*a21. – Dirk Feb 16 '13 at 13:44

4 Answers4

2

It's still a two dimensional array (int[,]) not a three dimensional array(int[,,]).

int[,] x = { { 3, 4, 5 }, { 3, 5, 6 }, { 5, 4, 3 } };

Side note: You can do that calculation for any multidimensional array like this:

int det_of_x = 1;
foreach (int n in x) det_of_x *= n;
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

You've declared a 3D array in the second example, not a 3x3 2D array. Remove the extra "," from the declaration.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
1

Because you have a 3 dimensional array and are using it like a 2 dimensional one such as x[0,0].

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
1

What you declared it a still two dimensional array. For 2D array you can use it like this;

int[,] x = { { 3, 4, 5 }, { 3, 5, 6 }, { 5, 4, 3 } };
int det_of_x = x[0, 0] * x[0, 1] * x[0, 2] * x[1, 0] * x[1, 1] * x[1, 2] * x[2, 0] * x[2, 1] * x[2, 2];
Console.WriteLine(det_of_x);
Console.ReadLine();

If you want to use 3 dimensional array, you should use it like int[, ,]

Check out more informations about Multidimensional Arrays from MSDN.

Since arrays implements IEnumerable and IEnumerable<T>, you can use foreach iteration on all arrays in C#. In your case, you can use it like this;

int[, ,] x = new int[,,]{ {{ 3, 4, 5 }, { 3, 5, 6 }, { 5, 4, 3 }} };
int det_of_x = 1;
foreach (var i in x)
{
    det_of_x *= i;
}

Console.WriteLine(det_of_x); // Output will be 324000

Here is a DEMO.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364