Double (or) Two dimensional arrays in C-language - C-Tutorial

Latest

Sunday, 9 October 2016

Double (or) Two dimensional arrays in C-language

Double (or) Two dimensional arrays

If the Array name followed by two subscripts then it is called two dimensional arrays.      


Declaration of Two dimensional arrays:
The general format of a two dimensional array is:
                        Syntax:           datatype ArrayName[size1][size2];
Where,
  • datatype specifies the type of the elements that will be stored in the array.
  • ArrayName specifies the name of the array that follows rules as a valid identifier.
  • size1specifies row size i.e., number of rows.
  • size2specifies column size i.e., number of columns
Example:        int a[3][4];

For this, memory allocation will be:


INITIALIZATION OF DOUBLE DIMENSIONAL ARRAYS

Two dimensional arrays can be initialized into two different ways. Those are
1. Direct initialization
2. Through program execution

Direct initialization:
1) Like one-dimensional arrays, double dimensional arrays can also be initialized by placing a list of values enclosed within braces as:
Syntax:           datatype ArrayName[size1][size2] = {List of Values};

            Here, List of Values is separated by comma operator.
                        Example:        int a[2][3] = {10,20,30,40,50,60};                 
           

           Through program execution:

/* PROGRAM TO READ A DOUBLE DIMENSIONAL ARRAY AND PRINT IT */

main()
{
            int m,n,i,j,a[10][10];
            clrscr();
            printf("\nEnter the row size:");
            scanf("%d",&m);
            printf("\nEnter the column size:");
            scanf("%d",&n);
            printf("\nEnter the Array Elements:");
            for(i=0;i<m;i++)
            {
                        for(j=0;j<n;j++)
                        {
                        scanf("%d",&a[i][j]);
                        }
            }
            printf("\nArray Elements Are:");
            for(i=0;i<m;i++)
            {
                        printf("\n");
                        for(j=0;j<n;j++)
                        {                     
                        printf("%5d",a[i][j]);
                        }         
            }

}

No comments:

Post a Comment