C# C# Collections Arrays Jagged Arrays

Rui Martinho
Rui Martinho
21,221 Points

C# Collection, jagged array challenge 1

My code is not validating, and i am receiving the below error message: "The table should contain 6 rows and 6 columns"

My code, is

" namespace Treehouse.CodeChallenges { public static class MathHelpers { public static int[][] BuildMultiplicationTable(int maxFactor) { int[][] arr = new int[maxFactor][];

       for (int i = 0; i < arr.Length; i++)
      { 
           arr[i] = new int[maxFactor];
           for (int j = 0; j <  arr[i].Length; j++)
        {
            arr[i][j] = new int();   
            arr[i][j] = i * j;
        }

       }

        return arr;
    }
}

} "

Can someone help?

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

           for (int i = 0; i < arr.Length; i++)
          { 
               arr[i] = new int[maxFactor];
               for (int j = 0; j <  arr[i].Length; j++)
            {
                arr[i][j] = new int();   
                arr[i][j] = i * j;
            }

           }

            return arr;
        }
    }
}

1 Answer

Steven Parker
Steven Parker
182,288 Points

:point_right: Your array is a bit too small.

Remember that the first row and column of the array will be 0, so to accommodate factors up to and including maxFactor, you'll need an array size of maxFactor + 1.

Rui Martinho
Rui Martinho
21,221 Points

Perfect.

It validated.

Thank you, Steven Parker