Data Structure 2D Arrays
Data Structure 2D Arrays
Two-dimensional array:
It is an array with „n‟ no. of rows & „m‟ no. of columns. Each element is accessed specifying
2 subscripts.
• ‟i‟ represents the row no. & „j‟ represents the column no. 2D arrays are called matrix
arrays
Representation of 2D arrays:
• The computer does not keep track of all the elements in the array.
• It will simply store the base address of the array i.e. the address of the first element
a[0][0].
• Let A be a 2D array of N rows & M columns.
• The array “A” may be stored in the memory in one of the following methods
❖ Row-major representation: In this representation, the first row of the array
occupies the first set of memory locations reserved for the array the second row
occupies the next set, and so on.
❖ Column-major representation: In this representation, the first column of the
array occupies the first set of memory locations reserved for the array the second
column occupies the next set, and so on.
• We use the following formula to retrieve the elements from the 2D array.
❖ Row-major representation:
Loc(A[I][J])= Base(A)+W*(M*I+J)
❖ Column-major representation:
Loc(A[I][J])= Base(A)+W*(N*J+I)
Where: N - no. of rows.
M - no. of columns.
Base(A) - Base Address
W - words per memory.
Consider an array:
a[0][0] a[0][1] a[0][2]
a[1][0] a[1][1] a[1][2]
1. Consider an array of 3x5 matrix. If a computer stores the array elements in row-major
order, then find the address of a[2][3]. Assume w=1, Base(a)=101.
Given: w=1, Base(a)=101, N=3, M=5, I=2, J=3
The formula to calculate is
Loc(A[I][J])= Base(A)+W*(M*I+J)
2. Consider an array of 2x6 matrix. Suppose base(marks)=400 & there are 4 words per
memory & the array is stored in both row-major order & col-major order, then find the
address of marks[1][4].
Given: w=4, Base(marks)=400, N=2, M=6, I=1, J=4
If the array is stored in row-major order, then the formula is:
Loc(A[I][J])= Base(A)+W*(M*I+J)
Substituting the given values in the above formula
= 400 + 4*(6*1+4)
= 400 + 4*(10)
= 400 + 40 = 440
Given: w=4, Base(marks)=400, N=2, M=6, I=1, J=4
If the array is stored in col-major order, then the formula is:
Loc(A[I][J])= Base(A)+W*(N*J+I)
Substituting the given values in the above formula
= 400 + 4*(2*4+1)
= 400 + 4*(9)
= 400 + 36 = 436
Applications of array:
1. It is used in matrix multiplication.
2. Arrays can be used in storing the elements.
3. It is used in the representation of polynomials.
4. It is used in CPU scheduling.
Advantages of array:
1. Array is capable of storing many elements at a time.
2. Data access is faster.
3. Arrays are simple to use.
4. Arrays can used to represent data structures such as stacks, queues, linked list, trees &
graphs.
Disadvantages of array:
1. Insertion & deletion are very difficult & time consuming because array elements are
stored in continuous memory location.
2. There is a chance of memory wastage or shortage.
3. We cannot change the size of the array at run time.
4. It can only store similar data elements.
*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*