Overview of 2 Dimensional array

A two-dimensional array is an array of array. 
For the best view, rotate your screen horizontally.
For example, consider a sample time table.



Here, the days are an array. Each day consists of an array within them. Thus it forms a 2-D array with 6 columns and 7 rows. The text entered in each box acts as an element.

The advantages and disadvantages of a 2-dimensional array are the same as a 1-dimensional array as I mentioned in this blog-1-D Array Overview.


A major application of 2-dimensional array is a representation of matrices. Let us consider an array arr with 2 rows and 3 columns.

8    5    4

2    8    3

Usually, a 2-D array is used for storing information that is visualized as a grid and table form. However, in memory, it is not stored as a table like visualization. It stores by continuous memory either of the following order:

1)      Row major order-

Here, elements of first rows are list first, followed by elements of the second row and so on. 

8

5

4

2

8

3

          arr(0,0)                arr(0,1)                arr(0,2)                arr(1,0)                arr(1,1)                  arr(1,2)

1)      Column major order-

Here, elements of first rows are list first, followed by elements of second row and so on.

8

2

5

8

4

3

          arr(0,0)                 arr(1,0)                 arr(1,1)                 arr(1,1)              arr(0,2)                arr(1,2)

The computer stores the address of the first element which is known as the base address i.e. address of arr(0,0). The rest of the addresses are calculated by the base address.

Let’s assume that an array arr has m number of rows and n number of columns. The size of an individual element is w. Now we want to find the address of element at i row and j column.

If array is stored as row major,

arr[i][j]=Base address +w[n(i-1)+(j-1)]

If array is stored as column major,

arr[i][j]=Base address +w[(i-1)+m(j-1)]


Comments

Popular posts from this blog

Overview of Tree Data Structure

Overview of Three Dimensional Array