TWO DIMENSIONAL ARRAYS There are 3 types of arrays:1. 1 D Array 2. 2 D Array 3. Multi – Dimensional Array • Maximum limit of Arrays is compiler dependent. If we want to represent an array in a matrix form we will be using 2 D Arrays. General form of an 2D array is Datatype
Ex:
int
array_ name[row_size][column_size];
i[4][3];
An array ‘i’ is declared which contains 12 integer values in 4 rows and 3 columns. Initializing a 2D array in program: int
i[4][3] = { { 1,2,3 } , { 4,5,6 } , { 7,8,9 } , { 10,11,12 } }; or
int
i[4][3] = { 1,2, 3,4, 5, 6 , 7, 8, 9,10,11,12 }; or
int
i[][3] = { { 1,2,3 } , { 4,5,6 } , { 7,8,9 } , { 10,11,12 } }; 16
NOTE: It is important to remember that while intialising an array it is necessary to mention the second(column) dimension, whereas the first dimension(row) is optional c1
c2
c3
r1
1
2
3
r2
4
5
6
r3
7
8
9
r4
10
11
12
4X3
MEMORY OF 2 D ARRAY: In memory it is not possible to store elements in form of rows and columns. Whether it is a 1 D (or) 2 D Array, the elements are stored in continuous memory locations. The arrangement of elements of a 2 D is shown below: int a[4][3] = = { { 10,20,30 } , { 4,8,9 } , { 23,41,32 } , { 15,18,24 } }; a[0][0] a[0][1] a[0][2]
a[1][0]
a[1][1] a[1][2] ……
…..a[3][2]
10 20 30 4 1000 1002 1004
8 9 1006
23 41 1008
1022
17
32 15 …………….
18
24
// WAP to read a 2-D Array and print it. Void main() { int a[10][10] , i , j , m , n ; Printf “enter the order of matrix \n ”); Scanf( “ %d%d ” , &m, &n); Printf “ \n enter the elements of array: \n ”); for(i=0;i