Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are the x,y and row, col attributes of a two-dimensional array backwards?

if I think of the x,y coordinate plane x,y is the common notation for an ordered pair, but if I use a two-dime array I have myArray[row][col] and row is the y and col is the x. Is that backwards or am I just thinking about it wrong? I was thinking it would look like myArray[x][y] but that's wrong if I want real rows and columns (like in a gameboard.) Wouldn't it be myArray[y][x] to truly mimic a row column board?

like image 230
johnny Avatar asked Feb 04 '10 21:02

johnny


People also ask

Are rows or columns 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".

How do you reverse a column in a 2D array?

An array object in NumPy is called ndarray, which is created using the array() function. To reverse column order in a matrix, we make use of the numpy. fliplr() method. The method flips the entries in each row in the left/right direction.

What are rows and columns in 2D arrays?

You can even store items in two-dimensional (2D) arrays which are arrays that have both rows and columns. A row has horizontal elements. A column has vertical elements. In the picture below there are 3 rows of lockers and 6 columns.

Are rows X or Y?

A group of rows and columns. The x-axis is the horizontal row, and the y-axis is the vertical column.


1 Answers

You have it right, and it does feel a bit backwards. the row number is a y coordinate, and the column number is an x coordinate, and yet we usually write row,col but we also usually write x,y.

Whether you want to write your array as [y][x] depends or [x][y] depends mostly on how much you actually care about the layout in memory (and if you do, what language you use). and whether you want to write functions/methods that can operate on rows or columns in isolation.

If you are writing C/C++ code, arrays are stored in Row Major Order which means that a single row of data can be treated as 1 dimensional array. But a single column of data cannot. If I remember correctly, VB uses column major order, so languages vary. I'd be surprised of C# isn't also row major order, but I don't know.

like image 69
John Knoeller Avatar answered Sep 22 '22 07:09

John Knoeller