Why is the memory of a 2d array accessed by two parameters and not just by one (ignoring pointers).Why is it that the memory diagram is in terms of rows and columns and not straight (horizontal),Further more why is it said that 2d array is a array of arrays but i dont get that .
It's about convenience. Sure, the memory's actually all sequential, but sometimes one wants to be able to access things with two indices (e.g. implementing matrices).
Consider a 3x3 array. It's convenient to think of the memory like this:
----------------------------
| [0][0] | [0][1] | [0][2] |
|--------------------------|
| [1][0] | [1][1] | [1][2] |
|--------------------------|
| [2][0] | [2][1] | [2][2] |
----------------------------
But in memory, it of course really looks like this:
----------------------------------------------------------------------------------
| [0][0] | [0][1] | [0][2] | [1][0] | [1][1] | [1][2] | [2][0] | [2][1] | [2][2] |
----------------------------------------------------------------------------------
We just split it up into rows so that we can easily understand it as two-dimensional. We access it with two parameters because we want to, because that's convenient for our code. The language provides this implementation, which makes it possible to access via two indices, even though under the covers it's linear and could be accessed with one index.
This picture should also help you understand why it can be considered an array of arrays. Here's a slightly modified picture, for emphasis:
|||--------------------------|||--------------------------|||--------------------------|||
||| [0][0] | [0][1] | [0][2] ||| [1][0] | [1][1] | [1][2] ||| [2][0] | [2][1] | [2][2] |||
|||--------------------------|||--------------------------|||--------------------------|||
As you can see, there are really three one-dimensional arrays there. So, when you write array[1]
, you are referring to the second one-dimensional component of the full two-dimensional array, i.e. the second group of three in memory. Adding a second index, array[1][2]
takes the third element of that one-dimensional array, getting you down to a single element of the two-dimensional array as desired.
its all about differences between:
1 2 3 4 5 6 7 8 9
[A][A][A][A][A][X][A][A][A]
and
1 2 3 4 5 6 7 8
1 [A][A][A][A][A][A][A][A]
2 [A][A][A][A][A][A][A][A]
3 [A][A][A][A][A][A][A][A]
4 [A][A][A][A][X][A][A][A]
5 [A][A][A][A][A][A][A][A]
6 [A][A][A][A][A][A][A][A]
find X in above data:
in the first one, in order to access to a address you just need ONE index (6 is X location)
but in the second one you have TWO index to identify a address (4 : 5 is X location).
you can consider second table as a array of first array.
Note that Memory Allocation for these arrays are different form what you seeing in my sample. My Sample is just for better understanding
It is the same reason why you don't write 2 dimensional matrix as a series of integers in your math notebook. It just means that they represent two different features. For e.g. the pixels on screen can be represented as a single array using A[i] { i => 0 to N^2 }
, but when somebody asks you for the 4th pixel on 10th row, you don't want to do the multiplication yourself everytime right? Instead you just return A[10][4]
.