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 (2×5):
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?
If you want row-major order, given row
rowIndex, columncolumnIndexand are faking (for lack of a better term) a two-dimensional array withnumberOfColumnscolumns, the formula isIf you want row-major order, given row
rowIndex, columncolumnIndexand are faking (for lack of a better term) a two-dimensional array withnumberOfRowrows, the formula isSo, assuming row-major order:
Output:
And if you insist on indexing using a single
forloop, assuming row-major order, the formula that you want is the following:If you’re using column-major order, the formula that you want is the following:
So,
will output
as expected.