C# C# Collections Arrays Multidimensional Arrays

Fredrik Rönnehag
Fredrik Rönnehag
2,341 Points

Multidimensional Array

I passed the exercise, but I'm not quite sure I did it the correct way. For an example, right now I don't use the int maxFactor anywhere, but I still passed.

I would rather learn the right way and understand the excercise correctly!

namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[,] BuildMultiplicationTable(int maxFactor)
        {
         int[,] array = new int[6, 6];

            for (int row = 0; row < array.GetLength(0); row++)
            {
                for (int col = 0; col < array.GetLength(1); col++)
                {
                    array[row, col] = row * col;
                }
            }
            return array;
        }
    }
}

1 Answer

Steven Parker
Steven Parker
171,327 Points

I'm guessing that while working on this, you got the error message "Bummer: The table should contain 6 rows and 6 columns". That was a bit too much information. It probably should have said "The table does not contain the right number of rows and columns".

A more correct solution would not contain the number "6" but would determine the sizes based on "maxFactor".

Fredrik Rönnehag
Fredrik Rönnehag
2,341 Points

Ah, so it should be int[,] array = new int[maxFactor, maxFactor]; ?

Yea, got that message so I changed the array to 6,6 and got a bit confused since it wasn't in the instructions.

Steven Parker
Steven Parker
171,327 Points

Close, but the dimensions need to be one more than the maximum factor (since index values start at 0).