views:

149

answers:

7
private int[, ,] table = new int[4 , 5 , 5]{
    {{0,6,2,6,4},{2,2,4,2,8},{4,4,8,4,6},{6,6,2,6,4},{8,8,6,8,2}},
    {{0,2,8,8,4},{2,4,6,6,8},{4,8,2,2,6},{6,2,8,8,4},{8,6,4,4,2}},
    {{0,4,2,4,4},{2,8,4,8,8},{4,6,8,6,6},{6,4,2,4,4},{8,2,6,2,2}},
    {{0,8,8,2,4},{2,6,6,4,8},{4,2,2,8,6},{6,8,8,2,4},{8,4,4,6,2}}
};

I want this table:

k|l     0      1       2      3      4

0       06264  22428  44846  66264  88682
1       02884  24668  48226  62884  86442
2       04244  28488  46866  64244  82622
3       08824  26648  42286  68824  84462

thanks for help

A: 

you need a 2 dimensional array, its [4,5] array

Microgen
A: 

Try skipping the new int[4 , 5 , 5] part, the compiler should be able to find out the dimensions. Otherwise I don't see any problem.

Thomas Wanner
A: 

The compiler can figure out the length of each dimension for you, so let it do all the hard work:

private int[,,] table = new int[,,]{
    {{0,6,2,6,4},{2,2,4,2,8},{4,4,8,4,6},{6,6,2,6,4},{8,8,6,8,2}},
    {{0,2,8,8,4},{2,4,6,6,8},{4,8,2,2,6},{6,2,8,8,4},{8,6,4,4,2}},
    {{0,4,2,4,4},{2,8,4,8,8},{4,6,8,6,6},{6,4,2,4,4},{8,2,6,2,2}},
    {{0,8,8,2,4},{2,6,6,4,8},{4,2,2,8,6},{6,8,8,2,4},{8,4,4,6,2}}
};

And this gives you the output you want, apart from the header:

    StringBuilder stringBuilder = new StringBuilder();

    for (int row = 0; row < table.GetLength(0); ++row)
    {
        stringBuilder.Append(row.ToString() + "\t");
        for (int column = 0; column < table.GetLength(1); ++column)
        {
            for (int valueIndex = 0; valueIndex < table.GetLength(2); ++valueIndex)
            {
                stringBuilder.Append(table[row, column, valueIndex].ToString());
            }
            stringBuilder.Append("\t");
        }
        stringBuilder.AppendLine();
    }

    string result = stringBuilder.ToString();
    Console.WriteLine(result);
Mark Byers
A: 

Just change new int[4 , 5 , 5] with new[,,]

this is what you should have :

var table = new[,,]
            {
                {{0, 6, 2, 6, 4}, {2, 2, 4, 2, 8}, {4, 4, 8, 4, 6}, {6, 6, 2, 6, 4}, {8, 8, 6, 8, 2}},
                {{0, 2, 8, 8, 4}, {2, 4, 6, 6, 8}, {4, 8, 2, 2, 6}, {6, 2, 8, 8, 4}, {8, 6, 4, 4, 2}},
                {{0, 4, 2, 4, 4}, {2, 8, 4, 8, 8}, {4, 6, 8, 6, 6}, {6, 4, 2, 4, 4}, {8, 2, 6, 2, 2}},
                {{0, 8, 8, 2, 4}, {2, 6, 6, 4, 8}, {4, 2, 2, 8, 6}, {6, 8, 8, 2, 4}, {8, 4, 4, 6, 2}}
            };
Yassir
+1  A: 

There's nothing wrong with your declaration. It compiles fine and I can write something like this:

        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                for (int k = 0; k < 5; k++)
                {
                    var x = table[i, j, k];
                }
            }
        }

Update: sure you can shorten it a tiny bit. Since the array dimensions are given by your initial values, you can write it like this:

        private int[,,] table = new [,,]{
             <your numbers go here>
        };
Peter Lillevold
Thanks, seems like a copy from VS to this post and then back again fixed the problem. :)(didn't actually change anything though.. =) )
Manne
Now that we have Linq, I'd like to use Enumerable.Range. Perhaps nice for those who don't know it: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx
Zyphrax
A: 

Nothing is wrong. It works flawlessly for me. What problem exactly you have?

silk
A: 

That piece of code won't compile if you have it in the wrong place - if you have it inside a function then drop the private modifier. (If it is declared at class level then it is fine).

slugster