Array Computer Solutions: Processing an Array: Traversal and Searching

Glide to success with Doorsteptutor material for UGC : Get complete video lectures from top expert with unlimited validity: cover entire syllabus, expected topics, in full detail- anytime and anywhere & ask your doubts to top experts.

Download PDF of This Page (Size: 316K)

Image of Processing an Array

Image of Processing an Array

Image of Processing an Array

Processing an Array

The various operations possible on arrays are:

Types of Processing an Array

Types of Processing an Array

Types of Processing an Array

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.

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”.

}

Developed by: