C# C# Collections Arrays Jagged Arrays

status 401
status 401
14,206 Points

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
8,714 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;
        }
status 401
status 401
14,206 Points

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

Marek Ott
Marek Ott
8,714 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)).