0

Is it possible to add one or more dimension to an existing array in c++? For example, I have 2 dimensional array, in somewhere I need to add one or more dimension to this array.

2 Answers 2

2

No matter what, as poitroae already mentioned, you will need a copy of some sort. This copy however would be sped up a great deal if you simulated 2-D and 3-D arrays with a 1-D array which is why I decided to post an answer in the first place.

In order to simulate 2-D and 3-D arrays with a 1-D array, you can use simple math to index into the array. This lowers memory segmentation making it faster due to having more chache hits. To add a dimension you would simply create another 1-D array with the proper size, and copy in your current data.

I will provide an example when I can find code where I have done this.

EDIT: Apparently the above is not quite what he wants. I will try again below

You have: a 1D array simulating a 2D array.

It seems like you do not want to actually add a whole dimension to the array, just the matrix. (basically resizing from one size to another while retaining data)

So, here is an example:

You have a 3x3 (U=3, V=3) matrix below

Logical Representation:

|3 5 6|
|7 2 5|
|1 0 2|

Physical representation( int[3*3] "matrix")

[3, 5, 6, 7, 2, 5, 1, 0, 2]

--Convert to a 4x4(X = 4, Y = 4)--

int* newMatrix = new int[X*Y];
for(int i = 0; i < Y; i++)
{
    for(int k = 0; k < X; k++)
    {
        //copy from old if it falls in bounds
        if(k < U && i < V)
        {
            newMatrix[i*X + k] = matrix[i*U + k];
        }
        //Otherwise, zero out
        else
        {
            newMatrix[i*X + k] = 0;
        }
    }
}
delete [] matrix;

That should give you a 4x4 newMatrix.

Logically:

|3 5 6 0|
|7 2 5 0|
|1 0 2 0|
|0 0 0 0|

Physically:

[3, 5, 6, 0, 7, 2, 5, 0, 1, 0, 2, 0, 0, 0, 0, 0,]

If you use variables like I did, you should be able to write a function that will do this for any matrix as long as you give it accurate parameters (the X and Y dimensions of the matrix)

To go the other way--downsize--it should be a similar set up (double for loop) where you check if you are in bounds and copy over only if you are. And if not, you simply do not copy anything. I will let you write that one.

I hope this is more what you are looking for. I jumped on dimension in the wrong context. Decided to think of an answer more in terms of matrices.

Sign up to request clarification or add additional context in comments.

13 Comments

Actually I don't know exactly how many dimension to add my matrix ahead of time. I will be waiting your example.
@DeanKnight this isn't what OP wants, I think, but you can actually do this legally, apparently
@UgurSahin is the total number of elements changing? if so, you can't get around reallocating: any language that lets you change the size of array is almost certainly reallocating and copying behinds the scenes for you...C++ won't do it for you automatically because it's trivial to do yourself. if the number of elements is not changing but the shape is, you can do what I linked to in the comment above (but I don't think that's what you want)
@UgurSahin if you don't know how many dimensions you need at compile-time, then how are you going to index the array? it's not impossible, I suppose, but you're going to have to build some kind of dynamic data structure yourself for it...built-in C++ arrays always have a compile-time constant number of dimensions.
@UgarSahin there's no way to have a single variable refer to a built-in array of unknown dimensionality in C++; it has to be statically known at compile-time; if you need that, then you need to create your own class which abstractly handles matrices of different dimensions and overloads operator[] to do what you want.
|
0

You cannot do this implicitly. You have to create a new array n-dimensional and copy the desired values

int new_array[10][10][10];

// copy using a loop or a std::-function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.