NIOS Computer Science: Chapter 11 – Array Computer Part 2

Download PDF of This Page (Size: 353K)

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

(1) 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;

}

(2) 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

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

}