Article No: 3
INTRODUCTION
In this chapter we will learn about array – a collection of similar data types. Elements of array contents are implemented by using consecutive memory locations. Arrays can be one-dimensional or multi-dimensional. In this chapter we will discuss about types of an array, operations on elements of an array.
In many situations, it may be possible that it is useful to collect the similar type of data items. C supports concept of array for this purpose. Let’s take an example of employee records:
1. Declare 25 different variables to store the age of employees.
2. Assign value to each variable.
It is also not easy to handle these variables in the program. The concept of array is useful in the situation. The type of data items may be char, int, or float. The elements of array share the same variable name. Specifying the array name followed by subscript in brackets indicates the elements of array.
WHAT IS AN ARRAY?
If we want to store a group of data together in one place then array is the one data structure we are looking for. This data structure enables us to arrange more than one element, that is why it is termed as composite data structure. In this data structure, all the elements are stored in contiguous (touched each other) locations of memory.
FIGURE 2.1 Array of data
An array is finite, ordered and collection of homogeneous (same) data elements. Array is finite because it contains only limited number of elements; and ordered, as all the elements are stored one by one in contiguous locations of computer memory in a linear ordered fashion. All the elements of an array are of same data type (say, integer) only and hence it is termed as collection of homogeneous elements. For example, an array of integers to store the age of all the students.
An array is known as linear data structure because, all elements of the array are stored in a linear data structure.
Declarations:
BASIC : DIMENSION A[100]
FORTRAN : DIM A[100]
Pascal : A: ARRAY[1…100] of integer
C : int A[100]
TERMINOLOGY
SIZE
Number of elements in an array is called the size of the array. It is also alternatively termed as length or dimension.
Type
Type of an array represents the kind of data type it is meant for. For example, array of integers, array of character strings.
Base
Base of an array is the address of memory location where the first element in the array is located. For example, 453 is the base address of the array.
Index
All the elements in an array can be referenced by a subscript like Ai or a[i], this subscript is known as index. Index is always an integer value. As each array elements is identified by a subscript or index that is why an array element is also termed as subscripted or indexed variable.
Range of index
Indices of array elements may change from a lower bound (L) to an upper bound (U), which are called the boundaries of an array.
In a declaration of an array in FORTRAN (DIMENSION A[100]), range of index is 1 to 100. For the same array in C (int A[100]) the range of index is from 0 to 99. These are all default range of indices. However in Pascal, a user can define the range of index for any lower bound to upper bound, for example, for A: ARRAY[-5 …19] of integer, the points of the range is -5, -4, -3, …., 18, 19. Here, the index of i-th element is -5 + i – 1. In terms of L, the lower bound, this formula stands as:
Index (Ai) = L + i – 1
If the range of index varies from L . . . U then the size of the array can be calculated as
Size (A) = U – L + 1
Word
Word denotes the size of an element. In each memory location, computer can store an element of the word size w, say. The word size varies from machine to machine such as 1 to 8 bytes. Thus, if the size of an element is double the word size of a machine then to store such an element, it requires two consecutive locations.
Resource Used:
1. Data Structure through C (By: D Samanta)
Compiled By: Chaudhary Amit V.

Comments
Post a Comment