# Two – Dimensional Arrays

2D Arrays- There could be  situations where a table of values will have to be stored. In such cases 1D arrays are of no use. So we use 2D arrays to represent the items in tables.  Therefore in 2D arrays two subscripts are used. One, for the rows of the table and second subscript for the columns of the table. In mathematics, we call it a matrix.  In C programming language two dimensional arrays are declared using the following syntax –

`                   type array_name [row_size][column_size];`

## Initializing 2D Array

Like the one dimensional array, 2D arrays can be initialized in both the two ways; the compile time initialization and the run time initialization.

Compile Time initialization – We can initialize the elements of the 2D array in the same way as the ordinary variables are declared. The best form to initialize 2D array is by using the matrix form.  Syntax is as below –

```int table-[2][3] = {
{ 0, 2, 5}
{ 1, 3, 0}
};```

This way is the best way to initialize the 2D array. It also increases the readability of the user.

Run Time initialization – As in the initialization of 1D array we used the looping statements to set the values of the array one by one. In the similar way 2D array are initialized by using the looping structure. To initialize the 2D array by this way, the nested loop structure will be used; outer for loop for the rows (first sub-script) and the inner for loop for the columns (second sub-script) of the 2D array. Below is the looping section to initialize the 2D array by using the run time initialization method –

``` for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&ar1[i][j]);
}
}```

### Sample 2D array Program

```/* Sample 2-D array C program */

#include<stdio.h>
#include<conio.h>
void main()
{
int array[3][3],i,j,count=0;

/* Run time Initialization */
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
count++;
array[i][j]=count;
printf("%d \t",array[i][j]);
}
printf("\n");
}
getch();
}```

Output
1      2      3
4      5      6
7      8      9

#### 9 thoughts on “Two – Dimensional Arrays”

1. zarra says:

can you help me…
given positive integers n and m, , generate m random permutations of the first n positive integers. sort each permutation using the insertion sort, counting the number of comparisons used. determine the average number of comparison used over all m permutation.

1. Belle says:

You get a lot of respect from me for writing these helpful arslicet.

2. Groumpff > Nan, pas de copyright pour Plaie-Mobile en ce qui me concerne, c’est free right pour qui n’en veut. *en revient toujours pas de voir son calembour reprit* Et fÃ©licitation ImpÃ©ratrice, je sais dÃ©j? ? qui j’offrirais votre oeuvre. A quand un livre raiponce ?

3. It’s a real pleasure to find someone who can think like that

2. aaa says:

My family members always say that I am killing my time here at net, but I know I am getting knowledge
every day by reading such pleasant articles or reviews.

3. aaa says:

you are in reality a excellent webmaster. The website loading speed is amazing.
It seems that you are doing any distinctive trick.
Also, The contents are masterwork. you have done a fantastic activity on this subject!

4. aaa says:

Thanks for finally writing about >Two – Dimensional Array <Loved it!

5. aaa says:

I believe that is among the most significant information for
me. And i am happy studying your article. However should remark on some common issues,
The site taste is ideal, the articles is actually excellent : D.

6. jocelyn says:

how to do this program:
Read in a series of numbers representing tst tubes and readings. The sentinel is
a single minus one. Your program should keep the product of the readings for each test tube. At the
end, it display the test tube numbers and product of each reading. Be sure not to display values for
test tubes that have no value. After this is done, display the minimum of the products. If the input were:
3 100
5 6
2 50
5 12
4 4
4 7
3 500
2 75
4 3
4 2
5 3
5 5
-1