Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting index of one dimensional array into two dimensional array i. e. row and column

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 the listbox 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);
    }
}
like image 734
Aabha Avatar asked May 28 '13 11:05

Aabha


People also ask

How do you convert a one-dimensional array to a two-dimensional array?

Use reshape() Function to Transform 1d Array to 2d Array The number of components within every dimension defines the form of the array. We may add or delete parameters or adjust the number of items within every dimension by using reshaping. To modify the layout of a NumPy ndarray, we will be using the reshape() method.

How do you convert a one-dimensional array to a two-dimensional array in C?

So the total number of elements of 1D array = (​ m * n ​ ) elements. Call the function​ ​ input_array​ to store elements in 1D array. Call the function ​ print_array​ to print the elements of 1D array. Call the function ​ array_to_matrix​ to convert 1D array to 2D array.

What does the two index values of a 2 dimensional array represent?

In this array, the position of data elements is defined with two indices instead of a single index. In Python, we can access two-dimensional array elements using two indices. The first index refers to the list's indexing and the second one refers to the elements' position.

Is row or column first in 2D array?

Java specifies arrays similar to that of a "row major" configuration, meaning that it indexes rows first. This is because a 2D array is an "array of arrays". The second illustration shows the "array of arrays" aspect.


1 Answers

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
like image 90
BambooleanLogic Avatar answered Oct 04 '22 05:10

BambooleanLogic