Converting index of one dimensional array into two dimensional array i. e. row and column
Solution 1
While I didn't fully understand the exact scenario, the common way to translate between 1D and 2D coordinates is:
From 2D to 1D:
index = x + (y * width)
or
index = y + (x * height)
depending on whether you read from left to right or top to bottom.
From 1D to 2D:
x = index % width
y = index / width
or
x = index / height
y = index % height
Solution 2
For converting 1D indices to and from 3D indices:
(int, int, int) OneToThree(i, dx, dy int) {
z = i / (dx * dy)
i = i % (dx * dy)
y = i / dx
x = i % dx
return x, y, z
}
int ThreeToOne(x, y, z, dx, dy int) {
return x + y*dx + z*dx*dy
}
Aabha
Updated on June 16, 2022Comments
-
Aabha almost 2 years
I have one application of
WinForms
which inside list box I am inserting name and price..name and price are stored in two dimensional array respectively. Now when I select one record from thelistbox
it gives me only one index from which I can get the string name and price to update that record I have to change name and price at that index for this I want to update both two dimensional array name and price. but the selected index is only one dimensional. I want to convert that index into row and column. How to do that?But I'm inserting record in list box like this.
int row = 6, column = 10; for(int i=0;i<row;i++) { for(int j=0;j<column;j++) { value= row+" \t "+ column +" \t "+ name[i, j]+" \t " +price[i, j]; listbox.items.add(value); } }