Arrays Notes About NET EXAM

Array



The ordered collection of identical elements is called an array. Sometimes, it is
required to store a large number of variables of the same type, one can use an
array. For example:
int A[5] ;
The above statement declares an array A which reserves 5 memory locations for
storing integer data type. These locations are A[0], A[1], A[2], A[3] and A[4]. The
number within the square bracket is called index or subscript. The array can be one
dimensional (one subscript) or two dimensional (two subscripts).


2 Objectives

After going through this lesson, you would be able to:
l explain the concept of an array
l define, access and initialize array elements
l perform common operations on arrays
l explain two dimensional array
l define, access and initialize two dimensional array elements

3 Initialization of One Dimensional Array

An array can be unitialized along with declaration. For array initialization it is required
to place the elements separated by commas enclosed within braces.
int A[5] = {1, 2, 3, 4, 5};
It is possible to leave the array size open. The compiler will count the array size.
int B [ ] = {6, 7, 8, 11, 10};

4 Initialization of String

An array of characters known as character string may be initialized by placing the
string in double quotes.
Char A [20] = “Computer Science”;
Char B [ ] = “Calcutta”;
A string is a series of characters stored in consecutive bytes of memory. This implies
that one can store a string in an array of characters, with each character kept in its
own array element. The last character of every C++ string is Null character. Therefore
char city [ ] = {‘c, ‘a’, ‘l’, ‘c’, ‘u’, ‘t’, ‘t’, ‘a’};
is an array of characters while
char city [ ] = {‘c, ‘a’, ‘l’, ‘c’, ‘u’, ‘t’, ‘t’, ‘a’, ‘\0’};
is a string because it is terminated by a null character

5 Processing an Array

The various operations possible on arrays are :
l Traversal
l Searching
l Sorting
l Insertion
l Deletion


Traversal

It means to access each location of an array, may be for display purpose. Consider
a program which will read five values from the user and finds out the maximum
value.

# include < iostream.h >
void main ( )
{ int T, A[5], l;
cout < < “Enter five values”;
for ( l = 0; l < 5; l ++)
cin > > A [ l ];
T = A [ 0 ];
for (l = 1; l < 5; l++)
{
if (T < A [l])
T = A [ l ];
}
cout < < “Maximum value” < < T;
}
Searching
This method finds out whether the data entered by the user is present in an array
or not. There are two types of searching method.

(i) Linear or Sequential search
(ii) Binary search
Linear or sequential search
This method is slower, inefficient and works on unsorted list. If the data we are
searching is not present in the list, we come to know at the end of the list.

/ / Linear search
# include < iostream.h >
void main ( )
{
int A[5], l, data, flag = 0;
cout < < “Enter five values”;
for (l = 0; l < 5; l ++)
cin > > A [l];
cout >> ‘Enter data to be searched”;
cin >> data;
for (l=0; l < 5; l ++)
{
if (A[l] = = data)
flag = 1;
}
if (flag = = 1)
cout < < “Data present”;
else
cout << “Data not present”;
}

Binary search

This method requires the array to be either in ascending or descending order. This
method calculates the mid location form initial and final locations and compares the
data with the value present in mid location. Consider the case where the list is in
ascending order. If data is less than a [mid] then data is present in the upper half
otherwise data is present in the lower half. The value of either final or initial will be
changed. The mid value is calculated again and searching continues till the data is
found or initial is greater than final. The values of initial, final and mid for searching
a value of 35 in an ascending order sorted list containing 9 elements is shown
below:
10 15 18 20 27 30 35 40 45
location (0) (1) (2) (3) (4) (5) (6) (7) (8)
Let the array have value: