C++ IMPLEMENTATION OF SEQUENTIAL/LINEAR SEARCH

Linear Search C Implementation      Linear Search C++ Implementation

/* sequential search or linear search
and print position */

#include<iostream.h>
#include<conio.h>
int lsearch(int a[],int n,int n1);
void main()
{
int a[30],i,n,n1,pos;
clrscr();
cout<<“Enter the total number of elements “;
cin>>n;
for(i=0;i<n;i++)
{
cout<<“enter the element “;
cin>>a[i];
}
cout<<“enter the element to search “;
cin>>n1;
pos=lsearch(a,n,n1);
if (pos==-1)
cout<<“Element is not present”<<endl;
else
cout<<“Element found at location “<<pos+1<<endl;
getch();
}
int lsearch(int a[],int n,int n1)
{
int i;
for(i=0;i<n;i++)
{
if (a[i]==n1)
{
return(i);
}
}
return(-1);
}

Linear Search C Implementation      Linear Search C++ Implementation

Number Of Visitors

0238239