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
172,328 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
172,328 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.