# Copying one dimensional array bottom to top

I want to convert a one dimensional array to a two dimensional array with the following conditions and have followed the code from this question - https://stackoverflow.com/questions/1817631/iterating-one-dimension-array-as-two-dimension-array. The 2-dimensional array has to be copied from the one dimensional array and written bottom to top. The second condition is that while the 1-dim array is being copied bottom to top the first row and the last column of the original 1-dimensional array must not appear in the two dimensional array if one were to visualize the 1-dim as a 2-dim array.

int maxrowIndex = rows-1;
for (int row = 0; row < rows; row++)
{
for (int column = 0; column < columns ; column++)
{
twodim[row][column] = onedim[maxrowIndex*columns+column];

}
maxrowIndex--;
}


Here is the second code

int maxrowIndex = rows-1;
for (int row = 0; row < rows-1; row++)
{
for (int column = 0; column < columns-1 ; column++)
{
twodim[row][column] = onedim[maxrowIndex*columns+column];

}
maxrowIndex--;
}


Did I get it right ? Is there any way I can improve the code ?

• the first row and the last column of the original 1-dimensional array how does a 1-dimensional array have rows and columns? I don't quite understand the question, could you provide an example? – cbreezier May 24 '15 at 7:53
• @user109486 - I meant if the 1-dimensional array is viewed as a 2-dimensional array. I have a data file(binary) that is stored in row major order. I have to read this data into a 1-d array , do a lot of post processing and write it out as 2 - dimensional array. Does that help? – gansub May 24 '15 at 8:04
• @user109486 - here is an example of how to translate 1 d array to 2 d - stackoverflow.com/questions/1817631/… – gansub May 24 '15 at 9:01

OK, so from what I can understand about your problem is that you want to "chunk" up a one-dimensional array into a two-dimensional array with rows rows and columns columns.

I'm going to assume row-major form since that's a little bit more typical (and seems to be what your code does too).

First of all, I don't understand the condition where it has to be written from bottom to up. I don't see any reason for this, but we'll assume we do have to do that for whatever reason.

Your code does not quite work, because you've confused the row variable and the maxrowIndex variable. In the part where you are attempting to calculate the one-dimensional index based on a given row and column you use this maxrowIndex variable which hints at it actually storing the current row index. But you do not use this when indexing your two-dimensional array.

In terms of style, I have to say that I'm not a fan of opening the brace { on the next line, and especially not indenting twice each time you nest.

Here's how I would approach the problem:

Edit: clarification - the data was original written north to south ( would have been the north west corner) and we need to flip vertically so that the new  should be the south west corner.

// We ignore the first row of the original array (which is the last row of the new 2d array
for (int row = 0; row < rows - 1; row++) {
// From first column to second last column (skips last column)
for (int column = 0; column < columns; columns++) {
int originalRow = rows - 1 - row;
twodim[row][column] = onedim[originalRow*columns + column];
}
}

• hm ok, so do you need twodim to be the origin? currently that will contain whatever was in onedim – cbreezier May 24 '15 at 15:00
• Yes I require twodim to be the origin and this has to be equal to onedim[rows*columns] – gansub May 24 '15 at 15:04
• Is that better? Now we've essentially flipped the data vertically and put it into a new 2d array. – cbreezier May 24 '15 at 15:12
• you have then introduced the extra variable :). Not an issue but the clarification is incorrect. The data is read bottom up and written bottom to top as well. Remember the original binary data file has row major ordering with the origin of the cell at the lower left hand corner. It needs to be read bottom up and then written bottom up as well. – gansub May 24 '15 at 15:18
• "lower left hand corner" is this  or [rows-1] in your representation? I will revert back to the previous version for now then. – cbreezier May 24 '15 at 15:25