# Introduction to Two Dimensional Array

So far in this post we introduce array data structure and in this post discussed one-dimensional array. In this post we discuss Matrix(two dimensional array) which is an example of multi-dimensional array.

**Two Dimensional Arrays**

Two dimensional arrays (alternatively termed matrices) are a collection of homogeneous elements where the elements are ordered in a number of rows and columns. An example of an \(m \times n\) matrix, where \(m\) denotes the number of rows and \(n\) denotes the number of columns. It is as follows:

$$

\begin{bmatrix}

a_{11} & a_{12} & a_{13} & \cdots & a_{1n} \\

a_{21} & a_{22} & a_{23} & \cdots & a_{2n} \\

\vdots & \vdots & \vdots & \vdots & \vdots \\

a_{m1} & a_{m2} & a_{m3} & \cdots & a_{mn} \\

\end{bmatrix}_{m\times n}

$$

The subscript of any arbitrary element, say \(a_{ij}\) represents the \(i^{th}\) row and \(j^{th}\) columns of the matrix.

**Memory representation of a Matrix**

- Row-Major Order
- Column-Major Order

In row major order, the elements of a matrix are stored in a row by row basis, that is, all the element in the first row, then all the element in the second row, and so on. On the other hand, in a column-major order, all the elements are stored column by column, that is, all the element in the first column, then in the second column, and so on. For example, consider a matrix **A** of order \(3 \times 4\):

$$

\begin{bmatrix}

a_{11} & a_{12} & a_{13} & a_{14} \\

a_{21} & a_{22} & a_{23} & a_{24} \\

a_{31} & a_{32} & a_{33} & a_{34} \\

\end{bmatrix}_{3\times 4}

$$

This matrix in the memory can be represented in the memory as shown below:

**References of elements in a Matrix**

Logically, a matrix appears as two dimensional; but physically it is stored in a linear fashion. So in order to map from logical view to physical structure, we need an indexing formula. Obviously, the indexing formula for a different order will be different. The indexing formula for different orders are stated below:

**Row-Major Order**

Assume that the base address is the first location of the memory is \(1\). So, the address of \(a_{ij}\) will be obtained as:

- Find the number of element in the first \( (i – 1)^{th}\) rows
- Find the number of elements in the \(i^{th}\) rows up to \(j^{th}\) column.
- Find the total element by summing result of the step 1 and step 2.

$$ \bbox[pink,5px]{address(a_{ij}) = (i-1)*n + j}$$

So, far the matrix \(A_{3 \times 4}\), the location of \(a_{32}\) will be calculated as \(10\). Instead of considering the base address to be 1, if it is at \(M\), then the above formula can be easily modified as:

$$ \bbox[pink,5px]{address(a_{ij}) = M + (i-1)*n + (j – 1)}$$

**Column-Major Order**

Let us first consider that the starting location of the matrix is at memory location \(1\). Then, to find the address of \(a_{ij}\) follow the following step:

- Find the number of elements in the first \(j-1)^{th}\) column.
- Find the number of elements in the \(j^{th}\) column up to \(i^{th}\) rows.
- Find the total element by summing result of the step 1 and step 2.

$$\bbox[pink,5px]{address(a_{ij}) = (j – 1)*m + i}$$

And considering the base address at \(M\) instead of \(1\), then the above formula will get modified as:

$$\bbox[pink,5px]{address(a_{ij}) = M + (j – 1)*m + (i – 1)}$$

**Application of Two Dimensional Array**

Some of the application of two-dimensional array are as follows:

**Image Processing****Computer Graphics****Graph Theory**

This article is contributed by Ram Kripal. If you like eLgo Academy and would like to contribute, you can mail your article to admin@elgoacademy.org. See your article appearing on the eLgo Academy page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.