C# C# Collections Arrays Multidimensional Arrays

Calvin Secrest
Calvin Secrest
20,761 Points

I don't think I quite grasp multidimensional arrays, and therefore am stuck on the challenge.

I'm still a little confused as about the syntax of the multidimensional arrays. I have this code, but it says the index was out of bounds. Can you explain to me what I'm doing wrong, and what I need to do to fix it without giving the answer away? BTW, here is the link to the challenge in case you need to look at it:

https://teamtreehouse.com/library/c-collections/arrays/multidimensional-arrays

Math.cs
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[,] BuildMultiplicationTable(int maxFactor)
        {
            var maxFactorPlusOne = maxFactor + 1;
            int[,] multiplicationTable = new int[maxFactor, maxFactorPlusOne];

            for (int row = 0; row < multiplicationTable.Length; row++)
            {
                for (int column = 0; column < multiplicationTable[row, maxFactorPlusOne]; column++)
                {
                    multiplicationTable[row, column] = row * column;
                }
            }
            return multiplicationTable;
        }
    }
}

1 Answer

Steven Parker
Steven Parker
172,140 Points

:point_right: You're very close, but you still have a couple of issues:

  • When sizing your array, both dimensions need to be maxFactorPlusOne.
  • Your row and column loops should also use maxFactorPlusOne as the limit in the condition expression.
Calvin Secrest
Calvin Secrest
20,761 Points

I change the row and column loops to maxFactorPlusOne and now I'm getting the error:

namespace Treehouse.CodeChallenges { public static class MathHelpers { public static int[,] BuildMultiplicationTable(int maxFactor + 1) { var maxFactorPlusOne = maxFactor + 1; int[,] multiplicationTable = new int[maxFactor, maxFactorPlusOne];

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

}