C# C# Collections Arrays Jagged Arrays

Daren Davis
Daren Davis
6,929 Points

Code seems valid... but getting "The table should contain 6 rows and 6 columns error" any suggestions?

test code with...

namespace CSharpcollections
{
    class Program
    {
        public static int[][] BuildMultiplicationTable(int maxFactor)
        {
            int[][] table = new int[maxFactor][];
            for(var i = 0; i < maxFactor; i++)
            {
                table[i] = new int[maxFactor];
                for(int j = 0; j < maxFactor; j++)
                    table[i][j] = i * j;
            }
            return table;
        }
        public static void Main()
        {
            var newTable = BuildMultiplicationTable(6);
            foreach (var row in newTable)
            {
                System.Console.WriteLine();
                for(int i = 0; i < row.Length; i++)
                {
                    System.Console.Write(row[i] + " ");
                }
            }
        }
    }
}
Math.cs
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[][] BuildMultiplicationTable(int maxFactor)
        {
            int[][] table = new int[maxFactor][];
            for(var i = 0; i < maxFactor; i++)
            {
                table[i] = new int[maxFactor];
                for(int j = 0; j < maxFactor; j++)
                    table[i][j] = i * j;
            }
            return table;
        }
    }
}

1 Answer

Steven Parker
Steven Parker
172,268 Points

:point_right: Your array is a bit too small.

To create a table that goes up to and including maxFactor, your loops should ues "less than or equal" ("<=") to compare instead of just "less than" ("<"). Also, since the array indexes start at 0, the sizes must be larger by one ("maxFactor + 1").

Daren Davis
Daren Davis
6,929 Points

Yep, got it thanx.