+ 2
how to write syntax of one and two dimensional arrays?
6 odpowiedzi
+ 25
As a little more comprehensive example I'd like to summarize the syntax in this way:
// declaring and initialize a 1D integer array
int arr1 [4] = {1, 2, 3, 4};
// declaring and initialize a 2D integer array
int arr2 [2][3] =
{
{1, 2, 3},
{4, 5, 6}
};
// declaring and initialize a 3D integer array
int arr3 [2][3][4] =
{
{ {1, 2, 3, 4}, {5, 6, 7, 8}, {9,10, 11, 12} },
{ {13,14,15,16}, {17,18, 19, 20}, {21,22,23,24} }
};
/////////////////
Accessing to each element of array:
// for 1D
arr1[0] = 10;
arr1[1] = 20;
arr1[2] = 30;
arr1[3] = 40;
// for 2D
arr2[0][0] = 10;
arr2[0][1] = 20;
arr2[0][2] = 30;
arr2[1][0] = 40;
arr2[1][1] = 50;
arr2[1][2] = 60;
// for 3D
arr3[0][0][0]; // 1
arr3[0][0][1]; // 2
arr3[0][0][2]; // 3
arr3[0][0][3]; // 4
arr3[0][1][0]; // 5
arr3[0][1][1]; // 6
arr3[0][1][2]; // 7
arr3[0][1][3]; // 8
arr3[0][2][0]; // 9
arr3[0][2][1]; // 10
arr3[0][2][2]; // 11
arr3[0][2][3]; // 12
arr3[1][0][0]; // 13
arr3[1][0][1]; // 14
....
+ 2
thnxxxx
+ 2
one dimensional would be like arr['1']
two dimensional would be like arr['2']['accounts']
arr['2'] is an array in this example not a variable
+ 2
Try
one dimension:
int d1[] = {1, 3 ,4};
two dimensions
int d2[,] = {2,49,596} , {63,84, 64};
+ 2
thank u :)
+ 1
thnx for ur help everyone:)