(Note: This will require multiple overloads of operator() with different numbers of index arguments.)
In C++, create a class for a 3-dimensional array. To make it more general, allow the actual dimensionality to be 0d, 1d, 2d, or 3d. It should support indexing with operator(). If the programmer subscripts with fewer indices than you 'need' (0 for a 1d, 0 or 1 for a 2d, 0 or 1 or 2 for a 3d -- note that you can't under-subscript a 0d array), you should return the appropriately sized sub-array (of your class's type). (Note: This will require multiple overloads of operator() with different numbers of index arguments.)
Since, as a template, math operations don't make sense, you'll have to for-go addition, etc. But you could use + and += for concatenation. Basically aligning the common dimensions of the two arrays and concatenating along the remaining dimension. (There can be only one for this to make sense. If there are none remaining, there are options for those kinds of things!) Make sure to overload it for proper dimensionality:
Of course, you should also support input (the programmer must set the dimensions ahead of time) and output (make it look nice like those above) and fun operations like transpose (operator~? but what is a 3d transpose...*ick*).
And how do you exactly store a dynamic array of more than one dimension? Well, the easiest way is to create a 1d array of all the items and then map the 3d subscripts into 1d:
actual dimensions:
x,y,z 3dX[i][j][k] --> 1dX[i*y*z+j*z+k]
Write a driver to show off your 3d (or less) class' abilities.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps