Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Collections Arrays Jagged Arrays

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,458 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;
        }
    }
}

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