Article No: 4
TYPES OF ARRAY
One-Dimensional Array
The simplest form of an array is one-dimensional array that may be defined as the finite as the set of homogeneous elements. If only one subscript/index is required to reference all elements in an array then the array will be termed as one-dimensional array or simply array.
Syntax: Data-type array-name [size];
Example:
int arr[10];
char name [20];
Array is a set of pair that is index and value. For each index there is value associated to it. Smallest index value is called lower bound and largest index value is called upper bound of an array.
Mapping
We can find out size of an array by subtracting lower bound from upper bound. That is,
Size = U – L + 1
Here, U is upperbound and L is lower bound.
Array elements can be accessed by a[0], a[1]. . . . .[n]. In C programming language array is starts with 0 subscript. In one-dimensional array if l = 1 then size of array will be upper bound. In one-dimensional array position of specific subscript element can be found by formula,
A= M + (i – 1) * W
M = starting address
i = the subscript of array
W = is the size of an element
Let us, take an example for it, in one-dimensional array a[20], find out the memory location of a[18], where array starts from memory location 1900 and size is 4.
M = 1900, W = 4, i = 20
A[20] = M + (i – 1) * W
A[20] = 1900 + (18 – 1) * 4
A[20] = 1968
Another example, an array A[-3…….8] is stored in memory whose starting address is 1024 word size of element is 2, find out the location for A[0] and A[5].
A[0]
A[0] = M + (i – L) * W
A[0] = 1024 + (0 – (-3)) * 2
A[0] = 1030
A[5]
A[5] = M + (i – L) * W
A[5] = 1024 + (5 – (-3)) * 2
A[5] = 1040
Two-Dimensional Array
Two-dimensional arrays (alternatively termed as matrices) are the collection of homogeneous elements where the elements are ordered in a number of rows and columns.
Syntax: data-type array-name [row][column];
Example:
int a[3][3];
char c[4][4];
OPERATIONS ON ARRAYS
1. Traversing
Input: An array A with elements
Output: According to PROCESS()
Data structures: Array A[L….U]
Steps:
i=L
while i<=U do
PROCESS A[i]
i=i+1
end while
stop
2. Searching
Input: KEY is the element to be searched.
Output: Index of KEY in A or a message on failure.
Data structures: An array A[L….U]
Steps:
i=L, found = 0, location = 0
while (i<=U) and (found = 0) do
if COMPARE(A[i], KEY) = TRUE then
found = 1
location = i;
else
i = i + 1
endif
endwhile
if found = 0 then
Print “ Search is unsuccessful : KEY is not found”
else
Print “Search is successful : KEY is in the array at location”, location
endif
Stop
3. Merging
Input: Two arrays A1[L1 . . . U1], A2[L2 . . . U2].
Output: Resultant array A[L . . . U], where L = L1, and U = U1 + (U2 – L2 + 1) when A1 is appended after A2.
Steps:
i1 = L1, i2 = L2
L=L1, U = U1 + (U2 – L2 + 1), i=L1
while (i1 < U1) do
A[i] = A1[I1]
i = i + 1, i1 = i1 + 1
endwhile
while (i2 < U2) do
A[i] = A2[i2]
i = i + 1, i2 = i2 + 1
endwhile
stop
Resourced Used: (Data Structure, by D Samanta)
Compiled By: Chaudhary Amit V.
Comments
Post a Comment