Ex.
No:02
LINEAR SEARCH
AIM: To write a C++ program to search a given elements using fuctions.
SAMPLE INPUT AMD OUTPUT:
OUTPUT:
#include<iostream.h>
#include<conio.h>
int Lsearch(int [],int,int);
void main()
int AR[20],ITEM,N,index;
clrscr();
cout<<"\nEnter Array size: ";
cin>>N;
cout<<"\NEnter Array Elements : ";
for(int i=0;i<N;i++)
cin>>AR[i];
cout<<"\nEnter Element to be searched : ";
cin>>ITEM;
index=Lsearch(AR,N,ITEM);
if(index==-1)
cout<<"\nSORRY!!! ELEMENT NOT FOUND";
}
else
cout<<"\nElement found at : (1)INDEX : "<<index<<"\n\t\t (2)POSITION : "<<index+1;
getch();
int Lsearch(int AR[],int SIZE,int ITEM)
for(int i=0;i<SIZE;i++)
if(AR[i]==ITEM)
return i;
return -1;
RESULT: Thus the given program is executed successfully and the output is verified.
OUTPUT:
Enter Array Size :
10
Enter Array Elements :
10
Enter Elements to be Searched : 6
ELEMENT FOUND AT : (1)INDEX : 5
(2)POSITION : 6