C# C# Collections Arrays Jagged Arrays

Stephen McClure
Stephen McClure
14,780 Points

Return a multiplication table consisting of an array of arrays. The table should contain all of the products of integers

This challenge question doesn't help me much for what's expected to solve this one. My code gives no compiler errors when I check it but I get the error: Bummer: Object reference...https://teamtreehouse.com/library/c-collections/arrays/jagged-arrays#preview

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

            for(int indexX = 0; indexX <= maxFactor; indexX++)
            {
                for(int indexY = 0; indexY <= maxFactor; indexY++)
                {
                    table[indexX][indexY] = indexX * indexY;
                }
            }

            return table;
        }
    }
}

2 Answers

Steven Parker
Steven Parker
204,000 Points

You're close, but:

  • the outer loop should create the inner array for the column and assign it to the table
  • the arrays need to be sized larger than maxFactor (since the indexes start with 0)
Stephen McClure
Stephen McClure
14,780 Points

Yeah I finally got it to work thanks. It stumped me for a while, but I just needed to take a break and then got it to work.