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 –


Sample 2D array Program

/* Sample 2-D array C program */

void main()
     int array[3][3],i,j,count=0;
     /* Run time Initialization */
                 printf("%d \t",array[i][j]);

1      2      3
4      5      6
7      8      9

yashan has written 69 articles

5 thoughts on “Two – Dimensional Arrays

  1. zarra says:

    can you help me…
    how about make this program:
    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.

  2. 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!

  3. 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.
    Just right task, cheers

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>