15

I have,

int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, index = 0;

as shown here, we create the the two dimensional one from the origin. But how do I iterate my oneDim inside for (index = 0; index < 10; index++) so that I could get my column index and row index there without creating a new one? I want it looks like this while printing its indexes to a two dimensional array (2x5):

0,0
0,1
1,0
1,1
2,0
2,1
3,0
3,1
4,0
4,1

I think the main issue here is getting the column index and row index without creating the two dimensional one. Don't you?

asked Nov 30, 2009 at 2:50
5
  • Huh? You want a one-dimensional array to look like a two-dimensional array? Commented Nov 30, 2009 at 2:53
  • dont quite get what you're after but, create an array of objects with column index and row index? Commented Nov 30, 2009 at 2:55
  • @Simon Righarts: This is standard. The compiler does it for you everyday. Commented Nov 30, 2009 at 3:49
  • tomiko, as you can see people are struggling to understand your question, not struggling to solve your problem. please review your question so that we don't have to strain our brains to understand you. i'm sure what you're trying to do is very simple, once you take the time to articulate your self i'm sure you'll get some meaningful help. Commented Nov 30, 2009 at 6:00
  • Okay psatanton. All that I want to ask is: "How do I transform/convert my one-dimensional array to a two-dimensional array?" Commented Nov 30, 2009 at 14:17

3 Answers 3

38

If you want row-major order, given row rowIndex, column columnIndex and are faking (for lack of a better term) a two-dimensional array with numberOfColumns columns, the formula is

rowIndex * numberOfColumns + columnIndex.

If you want row-major order, given row rowIndex, column columnIndex and are faking (for lack of a better term) a two-dimensional array with numberOfRow rows, the formula is

columnIndex * numberOfRows + rowIndex.

So, assuming row-major order:

int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int rows = 2;
int columns = 5;
for (int row = 0; row < rows; row++) {
 for (int column = 0; column < columns; column++) {
 System.out.println(row + ", " + column + ": " + oneDim[row * columns + column]);
 }
}

Output:

0, 0: 1
0, 1: 2
0, 2: 3
0, 3: 4
0, 4: 5
1, 0: 6
1, 1: 7
1, 2: 8
1, 3: 9
1, 4: 10

And if you insist on indexing using a single for loop, assuming row-major order, the formula that you want is the following:

int column = index % numberOfColumns;
int row = (index - column) / numberOfColumns;

If you're using column-major order, the formula that you want is the following:

int row = index % numberOfRows;
int column = (index - row) / numberOfRows;

So,

int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int rows = 2;
int columns = 5;
for(int index = 0; index < 10; index++) {
 int column = index % columns;
 int row = (index - column) / columns;
 System.out.println(row + ", " + column + ": " + oneDim[index]);
}

will output

0, 0: 1
0, 1: 2
0, 2: 3
0, 3: 4
0, 4: 5
1, 0: 6
1, 1: 7
1, 2: 8
1, 3: 9
1, 4: 10

as expected.

answered Nov 30, 2009 at 3:33

1 Comment

Did you mean "column"-major order on the second paragraph? Hate to make you edit after so many years. :-)
9

The two numbers you're showing could be computed, in the order you're showing them in, as index/2 and index%2 respectively. Is that what you mean by "the issue"?

answered Nov 30, 2009 at 2:54

Comments

3

I think this is what your trying to do...convert a one-dim array into a two-dim array.

//this is just pseudo code...not real syntax
int[10] oneDim = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int first_dim = 5;
int second_dim = 2;
int[first_dim][second_dim] new_array;
for (int fdi = 0; fdi < first_dim; fdi++){
 for (int sdi = 0; sdi < second_dim; sdi++) {
 //this is the crux...you're calculating the one dimensional index to access the value
 new_array[fdi][sdi] = oneDim[fdi*second_dim + sdi] 
 }
}
answered Nov 30, 2009 at 3:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.