C# C# Collections Arrays Multidimensional Arrays

yujia liu
yujia liu
14,964 Points

I got error "Index was outside the bounds of the array." I cannot figure out what wrong with my code

There are 4 rows and 4 columns, so I think index is fine

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

            for(int i=0; i< sheet.GetLength(0); i++)
            {
                for(int j=0;i< sheet.GetLength(1); j++)
                {
                    sheet[i,j] = i * j;
                }
            }
            return sheet;
        }
    }
}

1 Answer

Steven Parker
Steven Parker
172,127 Points

It looks like you just have a typo.

In your second loop, the index variable you initialize and increment is "j", but the one in the condition is "i".