C# C# Collections Arrays Jagged Arrays

I need to include the [0,0] row and column and I can't work out how.

I have coded up a basic multiplication matrix. However, to complete the challenge I need to include the first row and columns of 0. I have tried everything that I can think of. Any help to solve this (fairly simple issue I can't crack) is greatly received.

Thanks

Math.cs
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[][] BuildMultiplicationTable(int maxFactor)
        {
            int[][] multiplicationTable = new int[maxFactor][];
            for (int row = 1; row <= maxFactor; row++)
            {
                multiplicationTable[row - 1] = new int[row];

                for (int col = 1; col <= row ; col++)
                {
                    multiplicationTable[row - 1][col - 1] = row * col;
                }
            }
            return multiplicationTable;
        }

    }
}

1 Answer

Steven Parker
Steven Parker
204,000 Points

The overall function structure is good, but you have some values issues:

  • the loops should start at 0 instead of 1
  • the rows will need to have maxFactor plus one items
  • the columns will need to be the same size as the rows (instead of the row itself)
  • the indexes need to remain inside the range

Thank you again for your help, and thanks for allowing me to learn where I went wrong. Much appreciated.