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

Struggle to solve this challange

I know i need to create a jagged array. I will be happy if someone can break down the solution in order for me to understand

2 Answers

Marek Ott
Marek Ott
9,206 Points
      public static int[][] BuildMultiplicationTable(int maxFactor)
      {
            int[][] jaggedArray = new int[maxFactor + 1][]; // plus one because you have to include 0 row so there should be 4 rows in total. Here you initialize number of rows
            for (int rowNumber = 0; rowNumber < jaggedArray.Length; rowNumber++)
            {
                jaggedArray[rowNumber] = new int[maxFactor + 1]; //Here you initialize number of columns in specific row
                for (int columnNumber = 0; columnNumber < jaggedArray[rowNumber].Length; columnNumber++)
                {
                    jaggedArray[rowNumber][columnNumber] = rowNumber * columnNumber;
                    //Console.Write(jaggedArray[firstDimension][secondDimension] + " ");
                }
                //Console.WriteLine();
            }

            return jaggedArray;
        }

why u need to initialize number of rows for the first place ?

Marek Ott
Marek Ott
9,206 Points

You don't have to. It was my convention on naming. First dimension (first [] in jaggedArray) can be treated as rows or if you want as columns of matrix (https://en.wikipedia.org/wiki/Matrix_(mathematics)).