Page 48 - DCAP407_DATA_STRUCTURE
P. 48
Unit 3: Arrays
For the number array, the valid index range is from 0 to 24. Index is considered to be out-of-bounds, if
the index expression results in a value less than 0 or greater than the array_size.
Representation of Linear Arrays in Memory
We know that the memory of a computer is just a sequence of address locations. Let A be a linear array
in the memory of a computer. Figure 3.1 depicts computer memory addresses of the array A.
Figure 3.1: Computer Memory Addresses of Array A
100
101
102
103
104
The following notation is used for the address of an element of array A:
LOC (A[P]) = address of the element A[P] of the array A.
We know that the elements of A are stored in successive memory cells. The computer keeps track of the
address of A’s first element and not the address of every element. This is denoted by Base (A) and is
known as the base address of A. The address of any element of A is calculated using the following
formula:
LOC (A[P]) = Base (A) + w (P – lower bound)
Here, A is a linear array, w is the size of each element of the array A, LOC is a variable used to store the
location of the linear array, and P is the index of the element. The time required to calculate LOC (A[P])
is fundamentally the same for any value of P. Suppose the array A has the capacity to store 4 elements.
The size of the array elements is as shown below:
A[0] = 2 bytes
A[1] = 2 bytes
A[2] = 2 bytes
A[3] = 2 bytes
Hence, the memory occupied by Array A is 8 bytes.
Consider an array A having a base address of 100, so Base(A)= 100.
Let us now calculate the address of A[1]. Here, index of the element is, P=1, size
of each integer, w=2 and lower bound is 0.
The formula is: LOC (A[P]) = Base (A) + w (P – lower bound)
Therefore, LOC (A[1] = 100 + 2(1-0) = 102
LOVELY PROFESSIONAL UNIVERSITY 41