Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,083 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".