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

I've done this code in visual studio and it works, but I can't find the specific answer to solve the question.

I'm getting rather frustrated. The code I've typed into the box in the browser works fine - I've checked it in Visual Studio. I understand the problem, I can solve it, but the system won't accept my code because it seems to be looking for a very specific answer. This is annoying as there are multiple ways to solve this problem.

Math.cs
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[][] BuildMultiplicationTable(int maxFactor)
        {
            // Create a zero based array of size maxFactor x MaxFactor
            maxFactor = maxFactor + 1;

            int[][] array = new int[maxFactor, maxFactor];


            // loop over 0 to maxfactor in a nested loop to fill the array
            int ycount = 0;
            while (ycount < maxFactor)
            {
                int xcount = 0;
                while (xcount < maxFactor)
                {
                    array[xcount,ycount] = xcount * ycount;
                    xcount = xcount + 1;
                }
                ycount = ycount + 1;
            }
            // return the array
            return array;
        }
    }
}

1 Answer

Just at first glance, I see this:

int[][] array = new int[maxFactor, maxFactor];

If you're creating a jagged array, you need to have something like this:

int[][] array = new int[maxFactor][];

Within the inner loop, you'll need to initialize the inner array.

I don't know if you still need the help, but I figured this might help someone else, at least.