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 trialStephen Mooney
2,794 PointsCan someone please explain the basics of jagged arrays? i know i have to multiple the rows by the columns
help please
namespace Treehouse.CodeChallenges
{
public static class MathHelpers
{
public static int[][] BuildMultiplicationTable(int maxFactor)
{
}
}
}
3 Answers
Steven Parker
231,248 PointsPerhaps 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
7,744 PointsThis 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
7,744 PointsSteven 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
7,744 PointsSteven Parker it's all good man, no worries!
Stephen Mooney
2,794 PointsThanks 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
231,248 PointsThis 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.
Keith Whatling
17,759 PointsI 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.