2. array
2. array
A linear array is collection of homogeneous finite number of data elements that are stored contiguously in
memory. The elements in an array are accessed using an index number.
Size represents the size of the array, i.e. the total number of elements that can be stored in an array. The
smallest index number of an array is referenced by LB called lower bound and the largest index number
of an array is referenced by UB called upper bound.
size = UB - LB + 1
The value of LB can also be negative. It is not mandatory for the first element to be stored at index 1
always.
For example,
If LB = -4 and UB = 7, then size = UB - LB + 1; size = 7 - (-4) + 1; size = 7+4+1 = 12
0 56 208
1 7 212
2 216
3 220
4 224 [7]
Table 2.1
In the last table, the LB of the array is -2 and UB is 4. Hence the size of the array is 4 - (-2) + 1 = 7.
But there are only 4 elements in the array which is 3 less than the size of the array. The index number of
the last element in an array at any given moment of time is denoted by N.
Application of arrays
1. Creating a large number of data elements with single identifier and storing them contiguously in
memory.
2. Particularly useful for representing in memory data structures like stacks, queues, trees and graphs.
3. Easy and effective data structure for implementing operations like sorting, searching and merging.
The first element of the array can be accessed using base(a), where a is the name of any array. We can
access the memory location of kth element of the linear array using the following formulae:
where,
a is the name of any array
w refers to the word size or bytes per storage location for each element of the array. The value of w
depends on the type of the array. For an array of integer elements, the value of w is 2. For a float array,
the value of w is 4 and for char is it 1. In other words w represents the amount of memory taken by each
element of an array of a specified data type.
-1 11 102
0 56 104
1 7 106
2 26 108
3 54 110
4 11 112
Table 2.2
Let us consider an integer array (a) as shown in the last table. The base address base (a) is 100 as the
first element of the array is stored at memory location 100. The next element is stored at location 102
which means that the value of w is 2. Let us suppose we want to know the location of element k = 2;
As you can see in the figure as well, the element with index 2 is stored at memory location 108.
2.4 Basic operations on array
Traversing refers to visiting each element of an array exactly once and applying any specified process to
each element.
1. Repeat For I = LB to UB
3. Exit
ALGORITHM TRAVERSE_N (TRAVERSING AN ARRAY) Apply PROCESS operation
to the array(ARR) with lower bound LB and upper bound UB. It is assumed that the
array is NOT full and N points to the index number of last element in the array.
1. I = LB
4. I = I + 1
4. Exit
4. Set I = I – 1 [Decrement I by 1]
7. Exit
Third operation given in this section is to delete an element at given location from an array.
2. Repeat For I = K to N
3. Set ARR[I] = ARR[I+1] [Move the elements one position to the left]
5. Exit
According to the algorithm, I is initialized with 4 (i.e. K) and in the first iteration the element at index 5 is
shifted to index 4.
In iteration no. 2, the value of I which was incremented to 5 in first iteration results in shifting the element
at index 6 to index position 5.
In the last step of the algorithm, the value of N is decremented by 1 to make it 5 and hence makes the
element at position 6 irrelevant. The final array looks like this,
2-D array is a logical representation of array elements in the matrix form. The computer memory is single
dimensional or linear. The logical and physical mapping of linear array is same, but there are points to
consider in the logical and physical mapping of 2-D arrays. Logical representation is how the user views
the data elements and physical representation is how the elements are actually stored in memory. The
physical representation of 2-D array is discussed in the next section.
Like in linear array, the elements in 2-D array can be accessed using two index values. One represents
the row number and the other represents the column number of the element to be accessed.
Figure 2.1
The figure above shows the logical representation of 2-D array. The element are stored in the form of
matrix. Each element is indexed using two subscript values.
2-D array can be represented in memory using either of the two techniques:
Row-Major Order
Column-Major Order.
Row-Major Order- As already stated in the previous section, the computer memory is linear. Hence, the
2-D array is also physically stored in memory linearly. In case the row-major order is followed, the entire
first row is stored in memory allocated to 2-D array and then followed by 2nd row and so on.
Consider the following logical representation of 2-D array:
Figure 2.2
The first element of the array can be accessed using base(a), where a is the name of any array and is
always the element with row and column index value of 1 no matter which order of memory
representation you follow. We can access the memory location of [ I, j ] element of the 2-D array as
follows:
Row-Major Order
where,
a is the name of any 2-D array
I and J are the index number's of the array element whose location is to be accessed
w refers to the word size or bytes per storage location for each element of the array. The value of w
depends on the type of the array.
14 56 11
29 69 89
[1, 2] 56 102
[1, 3] 11 104
[2, 1] 29 106
[2, 2] 69 108
[2, 3] 89 110
As you can see in the figure as well, the element with index [1,3] is stored at memory location 104.
Column-Major Order
where,
I and J are the index number's of the array element whose location is to be accessed
w refers to the word size or bytes per storage location for each element of the array. The value of w
depends on the type of the array.
[2, 1] 29 102
[1, 2] 56 104
[2, 2] 69 106
[1, 3] 11 108
[2, 3] 89 110
Table 2.4
As you can see in the figure as well, the element with index [1,3] is stored at memory location 108.
2.7 Pointer
Pointer is used to store the base address of a memory reference or block. It can be used to store the
address of any element or node of a given data structure. For example, base is a pointer that points to the
base address of the array. Pointer can be of any basic data type and is from derived data type class.
Pointers are particularly useful in traversing the data structure. The concept of linked list data structure is
not possible without pointers. The link between two nodes of the linked list is maintained with the help of
the pointer. The address of the next node in the list is stored in the link part of the first node.
It is possible to perform basic arithmetic operations like addition and subtraction on pointer type. As
already stated, pointer is used to point to some memory location. Performing simple add operation on
pointer, makes it point to the block N blocks ahead of the current block it is pointing to and performing
simple subtract operation on pointer, makes it point to the block N blocks before of the current block it is
pointing to.
-1 11 102
0 56 104
1 7 106
2 26 108
3 54 110
4 11 112
Table 2.5
Let us suppose, pointer PTR is pointing to the array element with index 0 whose address is 104.
Adding 2 to the value of pointer as follows:
PTR = PTR + 2
Makes it point to the element with index no. 2 and new value of PTR is 108. The value addition to the
pointer is as follows;
where w is the size of the word or memory block which is two in this case as the array is of integer type
and integer takes 2 bytes in memory.
It is possible to store the base address of an array in any pointer and then access each element of the
array using the concept of pointer addition.
Array can also be created for pointer type. Type of pointer is eventually the type of pointer array. Array of
pointer is used to store a series of addresses in it. 2-D array is a good example of array of pointers.
In case of row-major order memory management, the row number for each row in the 2-D matrix points to
the base address of that row alone in programming and in case of column-major order memory
management, the column number for each column in the 2-D matrix points to the base address of that
column alone.
Hence the array uses the concept of static memory management whereby the memory is of fixed size and
is allocated to the data structure when the program is compiled. Program is a sequence of instructions
written in any programming language for the given algorithm.
Linked list is flexible in size. It starts with no nodes and depending upon the requirement, new nodes can
be added to the linked list and also removed from the linked list. The decision of adding nodes to the
linked list can be taken when the program is in execution.
The linked list follows the concept of dynamic memory management whereby memory to its nodes can be
allocated or de-allocated at any time. It is more useful then the static memory management technique as
it reduces the chances of memory wastage.