Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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)).