Page 22 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 22
Unit 1: Introduction to Data Structure and Arrays
or Space Required = (i – LB)*s …(3) Notes
If the Address of A[LB] is _ then the Address of ith element of Array will be given by:
Address of A[i] = _+(i – LB)*s …(4)
Now, we can write a Program for addressing the ith element of a single dimensional Array. We
can take _, i, LB and s as input from user and output the address of A[i].
Consider a One-dimensional Array of figure given below. It has five elements. Each element
takes 4 bytes to store.
Suppose Address of a[0]=1000, and we want to calculate the address of a then from the formula
4
(4) we have,
_ = 1000,
i = 4,
LB = 1,
s = 4
Address of a = 1000 + (4 – 1)*4
4
Address of a = 1012.
4
a
5
a
4
a
3
a
2
a
1
Memory Representation of Two-dimensional Array
Even though Multidimensional Arrays are provided as a standard data object in most of the
high level languages, it is interesting to see how they are represented in memory. Memory may
be regarded as one dimensional with words numbered from 1 to m. So we are concerned with
representing n dimensional Array in a one dimensional memory.
A two dimensional ‘m x n’ Array A is a collection of m.n data elements such that each element is
specified by a pair of integers (such as j, k), called subscripts, with property that
1 × j × m and 1 × k × n.
The element of A with first subscript j and second subscript k will be denoted by AJ,K or A[J, K].
Two dimensional arrays are called matrices in mathematics and tables in Business Applications.
Consider the following example of a 3 × 3 array.
Columns
1. A [1, 1] A [1, 2] A [1, 3]
2. A [2, 1] A [2, 2] A [2, 3]
3. A [3, 1] A [3, 2] A [3, 3]
Two-dimensional 3 × 3 Array A
LOVELY PROFESSIONAL UNIVERSITY 17