4
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

2
  • just new int[4 , 5] or did i miss something :>? Commented Jan 30, 2010 at 11:11
  • yup, it needs to be 3dimensional Commented Jan 30, 2010 at 11:11

7 Answers 7

2

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>
        };
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, seems like a copy from VS to this post and then back again fixed the problem. :) (didn't actually change anything though.. =) )
Now that we have Linq, I'd like to use Enumerable.Range. Perhaps nice for those who don't know it: msdn.microsoft.com/en-us/library/…
0

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

Comments

0

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.

Comments

0

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

Comments

0

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);

Comments

0

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}}
            };

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.