+ 1
Easy way to go through a 3 dimensional array?
I can keep up with 2d arrays but 3d arrays seem very confusing to go through with loops. Any suggestions or alternatives?
4 Réponses
+ 9
Multidimensional arrays are easy. Code the inner most array access first:
int arr[4][3][2]
for (i = 0; i < 2; i++)
arr[][][i] = 0;
Add the next level around it:
int arr[4][3][2]
for (j = 0; j < 3; j++)
for (i = 0; i < 2; i++)
arr[][j][i] = 0;
Continue adding nesting until done:
int arr[4][3][2]
for (k = 0; k < 4; k++)
for (j = 0; j < 3; j++)
for (i = 0; i < 2; i++)
arr[k][j][i] = 0;
+ 4
Try to imagine it as a structure.
1D Array: list of elements
2D Array: paper with rows and columns
3D Array: stack of papers with page numbers
4D Array: binder with multiple paperstacks
Or in a geometric way:
1D: line
2D: plane
3D: Cube
4D: lined cubes
5D: plane of cubes
6D: cube of cubes
I don't actually know if this helps you though :D
+ 3
the many dimensions u want to create, just open & close the same no. of [ ] braces.
+ 2
Thank you John Wells. You are awesome.