Computer Science Canada

Creating Multidimensional Array with a Nonconstant value

Author:  one [ Tue Jan 20, 2009 9:25 pm ]
Post subject:  Creating Multidimensional Array with a Nonconstant value

Variable inputted- a
The array is of integer type and 3 of the dimensions are known
Eg:
c++:
int array[5][5][5][a]

How would you do that?

For 2d array its like this:
c++:
int **a;
int rows=3,cols=4;//3X4 matrix
a=new int* [rows];
for(int i=0;i<rows;i++)
    *(a+i)=new int[cols];


So what would you do for 4d?

Thanks



Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="cpp"]Code Here[/syntax]

Author:  OneOffDriveByPoster [ Tue Jan 20, 2009 11:09 pm ]
Post subject:  Re: Creating Multidimensional Array with a Nonconstant value

If you understand the following declaration, then see what you can do with it:
c++:
int *array[5][5][5];

Author:  md [ Wed Jan 21, 2009 8:26 pm ]
Post subject:  RE:Creating Multidimensional Array with a Nonconstant value

you could use a typedef - always fun...

Author:  wtd [ Sat Jan 24, 2009 3:03 pm ]
Post subject:  RE:Creating Multidimensional Array with a Nonconstant value

You should be creating your own wrapper class so all of this munging about in three dimensional arrays is hidden from the main logic of your program.

A hint:

c++:
template <typename T>
class Matrix3D
{
    public:
        Matrix3D(size_t x, size_t y, size_t z, T init_val);

        // Add member functions as needed to manipulate and access "data".

    private:
        size_t x_size, y_size, z_size;

        T* data; 
};

template <typename T>
Matrix3D<T>::Matrix3D(size_t x, size_t y, size_t z, T init_val)
: x_size(x), y_size(y), z_size(z)
{
    // Initialize "data" here
}


: