OneCompiler

Linear search

150

#include<iostream.h>
#include<conio.h>
class linsearch
{
int n,a[100];
public:
void get();
void lsrch(int k);
};
void linsearch :: get()
{
cout<<"\nEnter the number of elements:";
cin>>n;
cout<<"\nEnter the elements of the list:";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
void linsearch :: lsrch(int k)
{
int count=0;
for(int i=0;i<n;i++)
{
if(a[i]==k)
{

cout<<"\nELEMENT FOUND";

count++;
break;
}
count++;
}
if(i==n)
{
cout<<"\nELEMENT NOT FOUND";
}
cout<<"\nNumber of comparisons="<<count;
}
void main()
{
int k1;
linsearch ln;
clrscr();
ln.get();
cout<<"\nEnter the element to be searched:";
cin>>k1;
ln.lsrch(k1);
getch();
}