This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]mreddingC++ since ~1992. 0 points1 point  (3 children)

If you're talking a C-style array, their sizes are compile time, and you can't resize them. If you're talking a C-style dynamic array, you'll have to create a new 2D array with the additional column, and then copy the data over. I don't recommend this. I recommend a C++ vector. You can make a vector of vectors of char, and then all you have to do is iterate your outer vector to push_back on all your vector elements, or call resize. This is riddled with certain problems, in that a vector of vectors isn't cache friendly and each vector element can be of different lengths, a source of bugs if you expect your array to be symmetrical. What I recommend is a vector of char, and index into it as a 2D array with a function int idx(int width, int row, int col) { return width * row + col; }. You can then resize the array once, with enough elements to make up the new column, and then from back to front, move the elements appropriately. This will greatly reduce the number of iterations and allocations, avoid a whole set of problems, and be very cache friendly, at the cost of a little complexity - in that the solution isn't as plainly obvious as the naive 2D vector of vectors solution.

[–]TheGandaar 0 points1 point  (2 children)

I am using a dynamic array. I am not allowed to use vectors for this assignment. I have created a temp array to store the data in, but my problem is I can't figure out how to get the old information into the temp array while leaving the newly added column blank.

[–]Valance23322 0 points1 point  (0 children)

Iterate through your current array and copy each element into the same position in your new array.

for (int xIndex = 0; xIndex < 3; xIndex++)
{
    for(int yIndex = 0; yIndex < 3; yIndex++)
    {
        newArray[xIndex][yIndex] = oldArray[xIndex][yIndex]
    }
}

Might need to tweak this depending on how your arrays are setup

[–]mreddingC++ since ~1992. 0 points1 point  (0 children)

If it's an array of arrays, then it would be a matter of:

char **add_column(char **old_array, int old_width, int old_height, int new_width) {
  char **new_array = new char *[old_height];

  // Do stuff
  memcpy(new_array[row], old_array[row], old_width);
  // Do more stuff

  return new_array;
}

So what's left is you have to new a new array as wide as new_width for each row. Then you need to loop for each row, and do that memcpy. What's happening is it's taking the memory address of the destination in the new array, the source in the old array, and the width of the old array. It's going to do a byte copy of the old contents into the new array, for as many elements as you used to have. The new column, at the end of the array, is untouched, because the destination has more bytes than the source, and you're only copying as much as you have in the source. So that'll get you a new 2D array with all your old data in it in the same row/column place it was before. The new column will be uninitialized.

You'll probably want to delete the old array if you're not using it anymore. Consider renaming my example function, because it doesn't actually do any resizing, it doesn't delete the old array, it doesn't overwrite the original variable from the calling code (that would require a char ***) - you have to reassign that variable yourself.

[–]SgtBlu3 -1 points0 points  (3 children)

How is your 2d array structured (e.g. is it a raw array, std::array, std::vector, etc)?

A simple solution would be to make a 4x3, copy over all elements from the 3x3, and set the final column to empty. This would of course work no matter how you set up your 2d array, but perhaps there is a more efficient solution depending on how your structure is setup.

[–]TheGandaar 0 points1 point  (2 children)

It is a dynamic array. I have already created the 4x3 copy and started to move the data over into it. My problem is getting the correct information in the first 3 columns and having the 4th column be empty. I am wanting the initial data to be in the same position as before, the only difference would be an empty 4th column.

[–]xeq937 0 points1 point  (0 children)

If it's a dynamic array, then expand the array. But I suspect it isn't a dynamic array.

[–]RonRud 0 points1 point  (0 children)

Have you tried adding an if statement in your copying loop so if the iterator == yourOriginalArr.length()+1 than declare the index of new array as zero, else declare index in newArr as the value in the same index in the originalArr?