C# C# Collections Arrays Jagged Arrays

Jason Finley
Jason Finley
12,501 Points

This should be producing a table with the same number of rows and columns as the maxFactor passed into it.

What am I doing wrong?

Math.cs
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[][] BuildMultiplicationTable(int maxFactor)
        {
            int[][] table = new int[maxFactor][];
            for (var rowIndex = 0; rowIndex < maxFactor; rowIndex++)
            {
                table[rowIndex] = new int[maxFactor];
                for (var colIndex = 0; colIndex < maxFactor; colIndex++)
                {
                    table[rowIndex][colIndex] = rowIndex * colIndex;
                }
            }

            return table;
        }
    }
}

2 Answers

Tim Strand
Tim Strand
22,345 Points

Basically your answer is correct but you are declaring an array the size of max factor when you really want an array 1 bigger than that ie maxFactor = 3 then 4x4 grid. If you just bump your array sizes by 1 then your logic should work. I also added a Console.WriteLine to demonstrate via the preview button that your logic works correctly.

namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[][] BuildMultiplicationTable(int maxFactor)
        {
            int arraySize = maxFactor + 1;
            int[][] table = new int[arraySize][];
            for (int rowIndex = 0; rowIndex != maxFactor; rowIndex++)
            {
                table[rowIndex] = new int[arraySize];
                for (int colIndex = 0; colIndex != maxFactor; colIndex++)
                {
                    table[rowIndex][colIndex] = rowIndex * colIndex;
                }
                System.Console.WriteLine(string.Join(",", table[rowIndex]));
            }
            return table;
        }
    }
}
Jason Finley
Jason Finley
12,501 Points

Thank you!! It kept telling me that it was expecting 6 rows and 6 columns but not telling me what the test input was.