0
using static System.Console;
namespace MultistoryFinder
{
    class MultistoryFinder
    {
        static void Main(string[] args)

        {
            int [,,] rents = { { 400, 500 }, { 450, 550 }, { 500, 550 } },
                             { { 510,610},{ 710,810},{ 910,1010} },
                             { { 525,625},{ 725,825},{ 925,1025} },
                             { { 850,950},{ 1050,1150},{ 1250,1350}  };

            int building;
            int floor;
            int bedrooms;
            String inputString;
            Write("Enter the building number");
            inputString = ReadLine();
            building = Convert.ToInt32(inputString);

  }
}

I am trying to create a program that accepts three-dimensional array but it keeps on giving me error that " nested array identifier is needed". I don't understand what to do in other to fixed the errors.

1
  • Please be specific regarding error messages. Do you get it when you run the program, compile it, give it water after midnight? Is there a specific line of code indicated by the error message? See Writing The Perfect Question. Commented Oct 2, 2016 at 3:10

3 Answers 3

1

The array initialization you are showing is for a 2 dimensional array. You need to use 3 dimensional array initialization. For example:

int[,,] rents = { { { 400, 500, 600 }, { 450, 550, 650 }, { 500, 550, 600 } } };

The above statement compiles with VS 2015.

Sign up to request clarification or add additional context in comments.

Comments

0

kindly try this code:

int[,,] rents = { 
                                { 
                                    { 400, 500, 600 }, 
                                    { 450, 550, 650 }, 
                                    { 500, 550, 600 }
                                }
                            };

            // Loop over each dimension's length.
            for (int i = 0; i < rents.GetLength(2); i++)
            {
                for (int y = 0; y < rents.GetLength(1); y++)
                {
                    for (int x = 0; x < rents.GetLength(0); x++)
                    {
                        Console.Write(rents[x, y, i]);
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
            }

            Console.ReadLine();

enter image description here

kindly refer to this link also: https://www.dotnetperls.com/multidimensional-array

Comments

0

As already stated in other answers you missed the outer braces.

Array initializers can be hard to visualize, so it is best to use a proper formatting.

In general you should always consider to start on a new line with your initializer, especially if a single line would be to long to read in your editor without horizontally scrolling.

An one dimensional array mostly has no problems:

int[] a1 = { 400, 500, 600 };

int[] a2 =
{
    400,
    500,
    600
};

Both of this representations can be easily understood.

For two dimensional arrays I suggest the following formatting for most of the times:

int a2d[,] =
{
    { 400, 500, 600 },
    { 450, 550, 650 },
    { 500, 550, 600 }
};

This can be easily read like a row/column based structure.

For short initializers it is acceptable to write it on only one line:

int a2d[,] = { { 400, 500, 600 }, { 450, 550, 650 }, { 500, 550, 600 } };

This is still readable, but only if it does not make the line too long.

Your three dimensional variant is a little bit more tricky, but not too hard:

int [,,] rents =
{
    {
        { 400, 500 },
        { 450, 550 },
        { 500, 550 }
    },
    {
       { 510, 610 },
       { 710, 810 },
       { 910, 1010 }
    },
    {
       { 525, 625 },
       { 725, 825 },
       { 925, 1025}
    },
    {
       { 850, 950 },
       { 1050, 1150 },
       { 1250, 1350 }
    }
};

With this formatting you can imagine your 3d array in a page/row/column semantic, just like an Excel file with many worksheeds.

In your special case, because you have only three "rows" and only two "columns" in your 3d array the following formatting is also well enough readable:

int [,,] rents =
{
    { { 400, 500 }, { 450, 550 }, { 500, 550 } },
    { { 510, 610 }, { 710, 810 }, { 910, 1010 } },
    { { 525, 625 }, { 725, 825 }, { 925, 1025} },
    { { 850, 950 }, { 1050, 1150 }, { 1250, 1350 } }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.