C# C# Collections Arrays Jagged Arrays

david harris
david harris
5,262 Points

Jagged Arrays.

I am currently working on jagged arrays in the c# collections. I am having an issue understanding jagged arrays. I know it is an array of arrays, but I do not know how to initialize them all. I need to make a multiplication table with a jagged array and just can't seem to do it. Can anyone help me?

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

            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
172,047 Points

You seem to be getting close, here's a few hints:

  • you know both dimensions of the array are the same
  • you know the dimensions need to be 1 larger than the maximum factor (because it starts at 0)
  • you need the same number of columns in each row, why stop the column loop at the row index?
  • you probably won't need a separate "row" array
  • you'll want to initialize each cell with the product of its row and column numbers
  • remember to return the multiplicationTable when the outer loop is finished
david harris
david harris
5,262 Points

Thanks for the help. I was thinking of stopping the columns with the rows because they were the same size. I wasn't fully understanding what was occurring in the for loops. I also didn't really know how to initialize the second array. Thanks again. Your answer worked great and your hints helped me understand it.