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

david harris
david harris
7,054 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
231,248 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
7,054 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.