0% found this document useful (0 votes)
9 views

Data Structure 2D Arrays

This chapter discusses two-dimensional arrays, which consist of rows and columns, and explains their representation in memory using row-major and column-major formats. It provides formulas to calculate the memory address of elements based on their indices and discusses the applications, advantages, and disadvantages of using arrays. Key applications include matrix multiplication and CPU scheduling, while challenges include difficulties in insertion and deletion.

Uploaded by

animaterealm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Data Structure 2D Arrays

This chapter discusses two-dimensional arrays, which consist of rows and columns, and explains their representation in memory using row-major and column-major formats. It provides formulas to calculate the memory address of elements based on their indices and discusses the applications, advantages, and disadvantages of using arrays. Key applications include matrix multiplication and CPU scheduling, while challenges include difficulties in insertion and deletion.

Uploaded by

animaterealm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Chapter 4 Data Structures- 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]

Row-major representation: column-major representation:

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)

St. Joseph’s PU College Page 1


Chapter 4 Data Structures- 2D arrays

Substituting the given values in the above formula ,


= 101 + 1*(5*2+3)
= 101 + 1*(10+3)
= 101 + 13 = 114

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.

*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*

St. Joseph’s PU College Page 2

You might also like