C# C# Collections Arrays Multidimensional Arrays

bot .net
bot .net
11,469 Points

Help with this challange

What is wrong with my code?

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


        }
    }
}

1 Answer

Daniel Medina
Daniel Medina
13,863 Points

Hello bot,

There are a few changes you can make to your code.

Instead of new int[maxFactor][], try this:

int[,] sheet = new int[maxFactor, maxFactor];  // format: x, y

Your first idea of using .Length makes sense given that this is an array. However, for multidimensional arrays, try using the .GetLength method. So sheet.GetLength(0) for your first dimension, sheet.GetLength(1) for your second dimension, etc.

Lastly, don't forget to return sheet outside of the for loops.

Other than these small tweaks, it looks great. Keep it up!


Some quick articles I found:

StackOverflow: https://stackoverflow.com/questions/9301109/how-do-you-loop-through-a-multidimensional-array

MSDN: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/multidimensional-arrays