C# C# Collections Arrays Jagged Arrays

Stephen Mooney
Stephen Mooney
2,462 Points

Can someone please explain the basics of jagged arrays? i know i have to multiple the rows by the columns

help please

Math.cs
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[][] BuildMultiplicationTable(int maxFactor)
        {

        }
    }
}

3 Answers

Steven Parker
Steven Parker
172,182 Points

Perhaps the most important thing to bear in mind about jagged arrays are that they are nested. So your master array will have other arrays inside it, each of which will need to be separately declared and initialized.

Also, remember that array index numbers start at 0, so the highest valid index will be one less than the array size (or perhaps I should say the array size needs to be one more then the highest index you will use). This will be handy for establishing loop limits as well as declaring the arrays.

Hopefully those hints will get you going.

David Tomko
David Tomko
7,741 Points

This one I struggled on too, but here is my code

namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[][] BuildMultiplicationTable(int maxFactor)
        {
            int[][] table = new int[maxFactor + 1][];
            for(int row = 0; row < table.Length; row++)
            {
                //here we are creating the actual rows of the array that have "maxFactor" + 1 columns
                table[row] = new int[maxFactor + 1]; 
                for(int col = 0; col < table[row].Length; col++)
                {
                    table[row][col] = row * col; // This is where we actually are giving the values to the array
                }
                System.Console.WriteLine(); //This creates a new line between every row in the array so everything isn't on one line
            }
            return table;
        }
    }
}
David Tomko
David Tomko
7,741 Points

Steven Parker the explanation is in the comments of my code. For me this is how I learn best if I have an issue that someone has already solved I like to see their code just so I can see the differences between theirs and mine. While also walking step by step through their code to see exactly what's going on.

David Tomko
David Tomko
7,741 Points

Steven Parker it's all good man, no worries!

Stephen Mooney
Stephen Mooney
2,462 Points

Thanks guys. Does anyone know of any business case a jagged array have been used for?

Finance or engineering maybe? But what about sme not in these areas?

Steven Parker
Steven Parker
172,182 Points

This is a pretty generic concept, I'm sure both jagged and multi-dimension arrays are used in all those and many other cases where data is organized in tables or a layered fashion.

I think a good example of jagged arrays would be an information send list. Say you had 10 users who wanted reports on the number of cars in their business, each user may have different a amount of cars. a Jagged Array would be a great collection to use to lookup the cars by user and send them the data for X car.