Two Dimensional Array


Two Dimensional Array can be represented using two subscripts.

Syntax
DataType ArrayName[Row-Size][Col-Size];
Here,
Row-Size indicates number of rows in an array.
Col-Size indicates number of columns in an array.
Example
int a[3][3];
Here,
a is an array having three rows and three columnsthat can store 9 values of type integer.

Number of elements that can be stored in two dimensional array can be  calculated using equation:
        Row_Size * Column_Size
Thus in an array of 3 rows and 3 columns we can store 9 elements.
Individual element of two dimensional array can be identify using an array name along with row-index and column-index. Row-Index and Column-Index in two dimensional array always starts with 0. So First element is a[0][0], second is a[0][1] and so on.

a[0][0] a[0][1] a[0][2]
a[1][0] a[1][1] a[1][2]
a[2][0] a[2][1] a[2][2]
     

Ammount of memory occupied by an array can be calculated using equation:
TotalMemory=RowSize*ColSize*Bytes Occupied By Data Type
Example:
float a[2][3];           
= 2 * 3 * 4 (float Occupies 4 Bytes) = 24 Bytes
int a[3][3];
= 3 * 3 * 2 (integer Occupies 2 Bytes) = 18 Bytes
Bydefault all the elements of two dimensional array are initialized to 0.
It is also possible to initialize all the elements of two dimensional array at the time of declaring array(Compile Time).

Syntax
DataType ArrayName[Row-Size][Col-Size]= {Values};
Example
int a[3][3]={1,2,3,4,5,6,7,8,9};

If number of values specified in curly bracket is less then the number of elements in an array then remaining elements are initialized with 0.
If number of values specified in curly bracket is greater then the number of elements in an array then compiler will generate error.
It is also possible to initialize an array at run time.

Example
int i,j,a[3][3];
for(i=0;i<3;i++)
{
    for(j=0;j<3;j++)
    {
         printf("Enter Value:");
         scanf("%d",&a[i][j]);
    }
}
Multi Dimensional Array

Multi Dimensional Array can be represented using more then two subscripts.

Syntax
DataType ArrayName[Dim1][Dim2]...[DimN];
Example
int a[2][3][4];
Here,
a is an array that can store 24 values of type integer.
 

Download Projects


Download Programs