C# C# Collections Arrays Multidimensional Arrays

Herman Vicens
Herman Vicens
12,540 Points

Same error here

Acording to the error message, I should be passing a 6 x 6 table, which is exactly what I get with the Console.Write statement in my screen. I don't know if there is a problen with the treeHouse site of there is something wrong that I can't see right now.

Math.cs
using System;
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[,] BuildMultiplicationTable(int maxFactor)
        {
            int newFactor = maxFactor+1;
            int[,] results = new int[10,10];
            for (int x=0 ; x != newFactor ; x++)
            {
                for (int y=0 ; y != newFactor ; y++)
                {
                    results[x,y] = x*y; 
                    Console.Write(" results["+x+","+y+"] ->"+results[x,y]);
                }
                Console.WriteLine();
            }
            return results;
        }
    }
}

3 Answers

Herman Vicens
Herman Vicens
12,540 Points

Andren, Thanks a lot. I did not know I could use a variable in the actual initialization of the array. That opens a world of other possibilities. Great to know. However, after I use the newFactor variable to initialize the array, I am still getting the Bummer ! [5][5] should contain 25.....and if I add the +1 to the newFactor = maxFactor + 1 then I get the Bummer!The table should contain 6 rows and 6 columns. Either way the challenge complaints and I ran our of ideas. Its easy to deal with compiler errors but the challenge is another whole story because it involves things like, taste, preferences, etc.

andren
andren
28,384 Points

That's strange, I tested the code in my answer before I posted it and it passed the challenge fine. So it should work correctly.

Maybe try resetting the challenge and copy/pasting the code I posted? The challenges can be a bit buggy at times.

andren
andren
28,384 Points

In this line:

int[,] results = new int[10,10];

You are hardcoding the size of the multidimensional array to be 10x10.

Even if you only fill up 6x6 the array is still technically 10x10, which is why the challenge complaints. Also this is not really just a nitpick of the challenge but an actual design flaw. Let's say the number 20 was passed to your method, what would happen then? Well the method would crash due to an "index is out of range" error.

If you make the size of the array dynamic in the same way your for loop is (you can use the same variable in fact) then your code will work.

Like this:

using System;
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[,] BuildMultiplicationTable(int maxFactor)
        {
            int newFactor = maxFactor+1;
            int[,] results = new int[newFactor,newFactor];
            for (int x=0 ; x != newFactor ; x++)
            {
                for (int y=0 ; y != newFactor ; y++)
                {
                    results[x,y] = x*y; 
                    Console.Write(" results["+x+","+y+"] ->"+results[x,y]);
                }
                Console.WriteLine();
            }
            return results;
        }
    }
}
Herman Vicens
Herman Vicens
12,540 Points

Done. Thanks a lot. Strange issue gone. I tried crazy stuff out of desperation in that challenge. It needed the reset. I appreciate your help. I learned good stuff from this one.