# 8.10 – Two Dimensional Array (Matrix)

by on November 2, 2017

### Two dimensional array or matrix in C language:

As we have discoursed in the previous sessions, an array is used to store multiple values of same type in a common memory allocation. Say for example, sales of a product in 12 months can be stored in a common memory allocation using an array.

Some times we need to store a table of data in a single variable. Say for example, we need a table of 5 rows and 12 columns to store the sales of 5 products in 12 months. We can define 5 arrays of 12 elements each to store the sales of 5 products in 12 months. Rather defining 5 arrays of 12 elements each, we can define a two dimensional array of 5 rows and 12 columns to store tabular data of 5 rows and 12 columns.

### Declaration of a two dimensional arrays:

Two pairs of brackets are used to declare a two dimensional array. Where number of rows is specified in the first pair of brackets and number of columns is specified in the second pair of brackets.

```int a[3][5];
```

The above statement allocates the memory allocation of 3 rows and 5 columns with the name a. Every element in a two dimensional array is accessed with its row index and the column index. Where index of rows and columns start from 0. If we take a two dimensional array of n rows and m columns then the index of last row is n-1 and the index of last column is m-1. A two dimensional array is also called as a matrix.

### Memory allocation of a two dimensional array:

C is a row major language. When a two dimensional array of n rows and m columns is declared then n single dimensional arrays of m elements each is allocated in adjacent memory allocations. Say for example, if we declare a two dimensional array a[3][5] then 3 single dimensional arrays of 5 elements each is allocated in the memory.

By knowing this we can define a two dimensional array as a set of single dimensional arrays of same size and type stored in adjacent memory allocations.

```int a[3][5];
```

The above picture shows that, a two dimensional array is a set of single dimensional arrays of same size allocated in adjacent memory allocations. Every element is accessed with its array (single dimensional) index and element index.

The same picture can be modified for our convenient as

Here, every single dimensional array is considered as a separate row and elements are considered as columns.

The same picture can be modified for our convenient as

Here, it appears like a table of elements arranged in rows and columns.

Conclusion:
A two dimensional array is a set of single dimensional arrays of same size and type allocated in adjacent memory allocations. For our convenient a two dimensional array is also defined as a table of data of same type arranged in rows and columns.

### Initializing while declaration of a matrix:

Assigning a set of sets of elements of same type while declaration of a two dimensional array is called initializing while declaration.

```int a[3][5]={{10,6,7,12,11},{23,32,14,52,22},{33,17,18,54,28}};
```

While initializing a matrix, it is optional to specify the size of rows but it is mandatory to specify the number of columns.

```int a[][5]={{10,6,7,12,11},{23,32,14,52,22},{33,17,18,54,28}};
```

It is also optional to specify the subset braces

```int a[][5]={10,6,7,12,11,23,32,14,52,22,33,17,18,54,28};
```

### Loops to traverse a matrix:

To access an element from the matrix, we need to specify the row index as the 1st subscript and the column index as the 2nd subscript. Say for example, a[1][2] access the element at 1’s row and 2’s column.

To traverse along the 0th row, the row subscript must be 0 and the column subscript must change for j=0, 1, 2, 3 and 4

```for(j=0;j<5;j++)           /* here 5 is number of columns */
printf("%5d",a[0][j]);
```

To traverse along the 1st row, the row subscript must be 1 and the column subscript must change for j=0, 1, 2, 3 and 4

```for(j=0;j<5;j++)           /* here 5 is number of columns */
printf("%5d",a[1][j]);
```

To traverse along the 2nd row, the row subscript must be 2 and the column subscript must vary for j=0, 1, 2, 3 and 4

```for(j=0;j<5;j++)           /* here 5 is number of columns */
printf("%5d",a[2][j]);
```

By observing all the three loops, we come to know that the same loop is being repeated except the changing in row index. To traverse 0th row it is a[0][j], to traverse 1st row it is a[1][j] and to traverse 2nd row it is a[2][j].

Here an outer loop can be used to select a row using for i=0, 1 and 2

Putting all together

```for(i=0;i<3;i++)  /* here 3 is number of rows */
{
for(j=0;j<5;j++)           /* here 5 is number of columns */
printf("%5d",a[i][j]);
}
```

Program:

```#include<stdio.h>
int main()
{
int a[][5]={{10,6,7,12,11},{23,32,14,52,22},{33,17,18,54,28}};
int i,j;
printf("Elements of matrix are:\n");
for(i=0;i<3;i++)              /* selecting rows */
{
for(j=0;j<5;j++)            /* traversing through the selected row */
printf("%5d",a[i][j]);
printf("\n\n");             /* Blank line between the rows */
}
return 0;
}
```

Output:
Elements of matrix are:
10    6    7   12   11
23   32   14   52   22
33   17   18   54   28

Execution explained:

Here when i is 0 then inner loop traverse the first row for j=0, 1, 2, 3 and 4 as a[0][0], a[0][1], a[0][2], a[0][3] and a[0][4]  which prints elements 10,6,7,12,11

When i is 1 then inner loop traverse the first row for j=0, 1, 2, 3 and 4 as a[1][0], a[1][1], a[1][2], a[1][3] and a[1][4] which prints elements 23,32,14,52,22

When i is 2 then inner loop traverse the first row for j=0, 1, 2, 3 and 4 as a[2][0], a[2][1], a[2][2], a[2][3] and a[2][4] which prints elements 33,17,18,54,28

### Common loop to access elements of a matrix:

```for(i=0;i<n;i++)   /* Here n is number of rows */
{
for(j=0;j<m;j++)  /* Here m is number of columns */
a[i][j]
}
```

### Common loop to store elements of a matrix:

```for(i=0;i<n;i++)   /* Here n is number of rows */
{
for(j=0;j<m;j++)  /* Here m is number of columns */
&a[i][j]
}
```

Previous post: