Multidemensional Arrays:
If the number of subscripts is more than one, then such Arrays are called Multi-dimensional Arrays that is we can say 2-D Array , 3-D Array and so on. The dimensionality is understood from the number of pairs of square brackets placed after the Array name.
Example:
- Array 1[ ] => 1-D Array
- Array 2[ ][ ] => 2-D Array
- Array 3[ ][ ][ ] => 3-D Array
Two-Dimensional Arrays: |
2-D Array generally referred to as a Matrix, of some Rows and some Columns. It is an Ordered tabel of Homogeneous elements. It is also called as a two-subscripted variable.
Declaration: |
The syntax of declaring a two-dimensional Array in C as follow:
datatype Array_name[rows] [columns] ; |
where
rows => number of elements to be processed under subscript 1
columns => number of elements to be processed under subscript 2
Example:
- int Marks[3] [5];
Marks is a 2-D Array of 3 rows and 5 columns.
- char Page[25] [80];
Page is a 2-D Array of 25 rows and 80 columns.
Initialization of 2-D Array: |
Like, initialization of 1-D Array elements, 2-D Array elements can also be initialized at the time of declaration. The syntax for a 2-D Array initialization is as follows:
data_type Array_name[size1] [size2] = {e1 , e2 ,....en}; |
where
e1 , e2 , ......en => initial values to be assigned to n elements of Array.
Example:
- int Matrix[3][3] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9};
Then, First 9 elements of the matrix will be
Matrix[0][0] = 1 Matrix[0][1] = 2 Matrix[0][2] = 3
Matrix[1][0] = 4 Matrix[1][1] = 5 Matrix[1][2] = 6
Matrix[2][0] = 7 Matrix[2][1] = 8 Matrix[2][2] = 9
Points to be Remember: |
- If the number of elements to be assigned are less than the total number of elements, then all the remaning elements are Automatically assigned to zero.
Here, ab is a 2-D Array of 2*2 elements of type int. But, only the first 3 elements are initialized. The fourth element is then automatically set to zero i.e
ab[0][0] = 2 ab[0][1] = 4
ab[1][0] = 6 ab[1][1] = 0
- If the number of elements to be assigned are more than the the total number of elemens that 2-D Array contained then there will be a compilation error.
- One to One mapping is preserved i.e the first set of initial values is assigned to the first Row elements and the second set of initial values is assigned to the second Row elements an so on.
Ex: int ary[2][2] = { {1,2} , {3,4} };
Here, the inner set {1,2} is taken for assigning values 1 and 2 to the first Row elements of an Array ary i.e
ary[0][0] = 1 ary[0][1] = 2
Similarly, the other set {3,4} is taken for assigning values 3 and 4 to the second Row elements of an Array ary i.e
ary[1][0] = 3 ary[1][1] = 4
That's All
That's All
0 Response to "Detailed Discription of 2-D Array of C Language[Tutorial]"
Post a Comment