Reputation: 41
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.
Upvotes: 0
Views: 228
Reputation: 1628
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 } }
};
Upvotes: 0
Reputation: 1909
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();
kindly refer to this link also: https://www.dotnetperls.com/multidimensional-array
Upvotes: 0
Reputation: 3837
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.
Upvotes: 1