I'm relatively new to programming, and I need to enter in a 4-dimensional array, and I can barely wrap my head around it. So, let's start with a simple 3-d array, 2 elements each like so:
int arr[2][2][2] =
{
{
{1, 2}
{3, 4} //redline on "{"
}
{ //redline on "{"
{5, 6}
{7, 8}
}
};
VS-2012 redlines the "{" before the "3", and says it's expecting a "}" instead. How would I be able to enter the array in a neat format? Having 4 dimensions will make it more complicated, and I need to be able to see the data clearly.
Also, I'm going to have a good number of zeroes in my array, in essense every arr[n][n] will be 0, so I'm wondering if I can make the initialization a little simpler.
my array will be of the type
int arr[7][7][15][2]
Or am I better off using struct instead?
Thanks in advance!
Each bracketed element can be viewed as one of the int
s inside { 1, 2 }
for example. Initializing with a list dictates that separate elements are enumerated with commas. The correct syntax thus will be with commas after each bracketed array ({ { 1, 2 }, { 3, 4 } }
):
int arr[2][2][2] =
{
{
{1, 2},
{3, 4}
},
{
{5, 6},
{7, 8}
}
};
For a four-dimensional array you'd have to write a lot of 'hardcoding' code; you're probably better off writing a function that will fill it out with values for you.
You're missing a ,
.
For a three dimensional array
int arr[2][3][4] = {
{
{1, 2, 1, 2}, {1, 2, 1, 4}, {1, 2, 4, 4}
},
{
{1, 1, 2, 4}, {1, 2, 1, 4}, {1, 2, 1, 4}
}
};
or int arr[2][3][4] = {1,2,1,2,1,2,1,4,1,2,4,4,1,1,2,4,1,2,1,4,1,2,1,4};
For a four dimensional array
int arr[2][3][4][2] = {
{
{
{1,2},{1,2},{1,2},{4,2}
},
{
{2, 4},{1, 4},{1, 4},{1,2}
},
{
{2, 4},{1, 4},{1, 4},{1,8}
}
},
{
{
{1,2},{1,2},{1,2},{4,2}
},
{
{2, 4},{1, 4},{1, 4},{1,2}
},
{
{2, 4},{1, 4},{1, 4},{1,2}
}
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With