C# C# Collections Arrays Multidimensional Arrays

Bad Array Declarator

I have tried more than a dozen different configurations of the array declaration and placement, but I keep getting the same error message about bad array declarator. No matter where I put the brackets, what I put in the brackets... nothing seems to make any difference. I've watched the videos a million times. I just don't understand what the hell is going on.

Math.cs
namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[,] BuildMultiplicationTable(int maxFactor)
        {
            int m1= maxFactor+1;

            for(int rowIndex = 0; rowIndex < m1; rowIndex++)
            {
                for(int colIndex = 0; colIndex < m1; colIndex++)
                {
                    int[,] BuildMultiplicationTable1[rowIndex,colIndex] = rowIndex*colIndex;
                }
            }   
            return BuildMultiplicationTable1;
        }
    }
}

2 Answers

Brent Phillips
Brent Phillips
Front End Web Development Techdegree Student 12,627 Points

Hi Emily,

You are very close to the solution! Allow me to try and help you through it:

The problem, as you pointed out, is in your array declaration: You are trying to initialize the array within the loop, which won't work. Every time the loop hits your array declaration it tries to re-initialize it, over and over again. All of your declarations need to happen outside of the loop. So, declare your array right below your 'm1' declaration. Don't forget to use your 'new' keyword when initializing your array, and remember that we are building a matrix, so your rows and columns will be the same length(Hint: your 'm1' variable will come into play here).

Next, your loop looks great. At the bottom level, we are no longer initializing the array, only modifying it. Drop the 'int[,]' and your logic will work fine. Finally, don't forget to make sure your return statement returns the correct name of your array.

Give it another shot and let me know how it goes.

Zain Sra
Zain Sra
3,232 Points

Emily the thing is that you're creating BuildMultiplcationTable1 Multidimentional Array inside inner for loop so its scope is limited to that and outside the { } braces of inner for loop it can't be accessed.

Secondly , before using such arrays You need to declare and initialize them with "new" keyword.

So in order to make it work, Do following steps:

  1. Initialize your Array properly outside the loops , after the function.
int[,] BuildMultiplicationTable1 = new int[4,3]; //It will create an array of 4 rows and 3 columns
  1. Use the above created array just like a normal variable, rest of your code works perfect !

Here's your modified code and it works in the challenge now !

namespace Treehouse.CodeChallenges
{
    public static class MathHelpers
    {
        public static int[,] BuildMultiplicationTable(int maxFactor)
        {
             int m1= maxFactor+1;
             int[,] BuildMultiplicationTable1 = new int[maxFactor+1,maxFactor+1];

            for(int rowIndex = 0; rowIndex < m1; rowIndex++)
            {
                for(int colIndex = 0; colIndex < m1; colIndex++)
                {
                    BuildMultiplicationTable1[rowIndex,colIndex] = rowIndex*colIndex;
                }
            }   
            return BuildMultiplicationTable1;
        }
    }
}

I hope it answers your question , Happy Coding !