Sunday, 2 September 2012

Linear Search


#include<iostream.h>
#include<conio.h>

class linear
{
private:
    int a[20],n,key,count;
public:
    void read();
    void search();
};
void linear::read()
{
    cout<<"Enter how many numbers : ";
    cin>>n;
    cout<<"Enter the numbers : ";
    for(int i=0;i<n;i++)
    cin>>a[i];
}
void linear::search()
{
    cout<<"Enter an element you want to search : ";
    cin>>key;
    count=0;
    for(int i=0;i<n;i++)
    {
    if(a[i]==key)
    {
    cout<<key<<" is at "<<i+1<<"th position"<<endl;
    count=1;
    }
    }
    if(count==0)
    {
    cout<<"The element "<<key<<" is not found";
    }
}
void main()
{
linear l;
clrscr();
l.read();
l.search();
getch();
}

No comments:

Post a Comment