Easy way to go through a 3 dimensional array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

19th May 2018, 11:25 AM
Akib
Akib - avatar
4 Answers
+ 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;
19th May 2018, 11:45 AM
John Wells
John Wells - avatar
+ 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
19th May 2018, 11:43 AM
Alex
Alex - avatar
+ 3
the many dimensions u want to create, just open & close the same no. of [ ] braces.
19th May 2018, 11:43 AM
Mayank Rampuriya
Mayank Rampuriya - avatar
+ 2
Thank you John Wells. You are awesome.
19th May 2018, 11:49 AM
Akib
Akib - avatar