0

I'm useing a method in Java, but I want it in C# too. It is used for tell if a matrix of int is transitive or not.

static boolean isTransitive(boolean[][] matrix) {
    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix.length; j++) {
            if (matrix[i][j]) {
                for (int k = 0; k < matrix.length; k++) {
                    if (matrix[j][k] && !matrix[i][k]) {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}

My main problem is, that I dont know what matrix[i][j] in C# means?

Is it possible for the code to work with this 4x4 of int array?:

transNumbers[0, 0] = 0;
transNumbers[0, 1] = 1;
transNumbers[0, 2] = 0;
transNumbers[0, 3] = 0;
transNumbers[1, 0] = 1;
transNumbers[1, 1] = 0;
transNumbers[1, 2] = 1;
transNumbers[1, 3] = 0;
transNumbers[2, 0] = 0;
transNumbers[2, 1] = 0;
transNumbers[2, 2] = 0;
transNumbers[2, 3] = 1;
transNumbers[3, 0] = 0;
transNumbers[3, 1] = 0;
transNumbers[3, 2] = 0;
transNumbers[3, 3] = 0;
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Knaks
  • 209
  • 3
  • 11
  • Are you asking how matrices are declared in c#? – Tony Ennis Jun 03 '12 at 14:23
  • are you asking how to declare and use multidimensional arrays in C#? – Jesper Fyhr Knudsen Jun 03 '12 at 14:24
  • 2
    That code should compile without significant changes is C#. I think you only need to uppercase `length`. – CodesInChaos Jun 03 '12 at 14:24
  • One could consider switching to solid 2D arrays `bool[,]` instead of `bool[][]`. But in some situations that actually reduces performance. – CodesInChaos Jun 03 '12 at 14:26
  • _"My main problem is, that I dont know what matrix[i][j] in C# means?"_ The same as in Java! http://msdn.microsoft.com/en-us/library/ms228389%28v=vs.80%29.aspx – Tim Schmelter Jun 03 '12 at 14:31
  • See update. I need this code to work with a 4x4 int array in C#. – Knaks Jun 03 '12 at 15:49
  • It doesn't. But then again, neither does it in Java. The type `boolean` in Java or `bool` in C# only knows two values, `false` and `true`. You specifically cannot assign numbers to boolean variables. Are you sure you actually know what you want? – Wormbo Jun 03 '12 at 19:24

5 Answers5

0

matrix[i][j] means exactly the same thing in C# as it does in Java.

That said, your matrix appears to be a matrix of booleans, not of ints as you describe!

ekolis
  • 6,270
  • 12
  • 50
  • 101
0

A multidimensional array is in C# defined with a , per dimension.

FOR EXAMPLE

int[,] test = new int[list1.Count, list2.Count]

http://bytes.com/topic/c-sharp/answers/531671-array-matrix-definition

robert
  • 33,242
  • 8
  • 53
  • 74
  • [What is the difference between multidimensional and jagged arrays in C#?](http://stackoverflow.com/questions/597720/what-is-differences-between-multidimensional-array-and-array-of-arrays-in-c) – Tim Schmelter Jun 03 '12 at 14:29
  • http://stackoverflow.com/questions/597720/what-is-differences-between-multidimensional-array-and-array-of-arrays-in-c – Nitesh Bhargava Jun 03 '12 at 14:31
0

Since you are using a boolean matrix, therefore if (matrix[i][j]) in your code means if the boolean element at that particular index [i][j] is true or not.

to break it up more simply:

consider an array:

[1][2][3]

[4][5][6]

[7][8][9]

Every element can be accessed with it's index. So, say element 5 has the index [1][1].(remember,indexes start from 0,not 1).

And you can iterate through arrays referencing your indexes using variables like i and j in your case.

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
0

Just instead of [][] use it like [I,j] . Rest of the code is fine.

Bohn
  • 26,091
  • 61
  • 167
  • 254
0

To translate your code to C#, all you have to to is replace boolean with bool. Everything else is identical in the two languages.

However, C# offers additional features you might want to implement. One of them is the existence of real multidimensional arrays. These implement the several dimensions of the matrix in a single object, while the [][] notation actually is a jagged array, i.e. an array of arrays.

Wormbo
  • 4,978
  • 2
  • 21
  • 41