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

Herman Vicens
Herman Vicens
12,540 Points

Don't know how to return the results. The array is not passing...

I am getting the TreeHouse " BuildMultiplicationTable returned null" error and I've tried putting [], and [][], and many other creative ways but keep getting the error. From the Console.Write I can tell that I am getting the desired results. But I don't know how to pass them.

Math.cs
using System;

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

            int[][] results = new int[100][];
            for(int x=0 ; x<maxFactor ; x++)
            {
                results[x] = new int[maxFactor]; 
                for(int y=0 ; y<maxFactor ; y++)
                {
                    results[x][y] = x*y;
                    Console.Write("  results[" + x + "][" + y +"] = " + results[x][y]);
                }
                Console.WriteLine();
            }
            return results;

        }
    }
}

3 Answers

Steven Parker
Steven Parker
231,269 Points

You're pretty close.

Here's a hint: the size of the result table should be maxFactor+1 in both dimensions.

Herman Vicens
Herman Vicens
12,540 Points

Thank you Steven. I tried that and gives me an index out of bounds message. I am printing the correct amount of numbers with the Console.Write so in my opinion, the program is doing what it is suposed to. But for some reason, the return statement at the end of the method is not returning the values of the array. Any idea why?

Herman Vicens
Herman Vicens
12,540 Points

I'm sorry Steven. You were right. I've overlooked the +1 in the instanciation of the second dimention and there is were I was getting the out of bounds message. I fixed that but now I am getting the "you should be getting 6 colums and 6 rows." and that is exactly what I am displayng with the Console.Write statement. Still stuck!! Thanks,

Steven Parker
Steven Parker
231,269 Points

When you expanded your dimensions, did you also expand your loops to fill them? If that's not it, perhaps you could show the whole code as it is now.