Linear Search


/********************************************************

-> This C++ program is to implement linear search algorithm

-> This program works in Microsoft vc++ environment

*********************************************************/

#include<iostream.h>

class searching
{
private:
 double *array;
 int n;
public:
 void input();
 void linearsearch();
};

void searching::input()
{
 cout<<“****************************************************\n”
  <<“This program is to implement linear search algorithm\n”
  <<“****************************************************\n”;
 cout<<“Enter how many numbers you are going to enter::”;
 cin>>n;
 array=new double[n+1];
 cout<<“Now enter your elements ::\n”;
 for(int i=1;i<=n;i++)
  cin>>array[i];
}

void searching::linearsearch()
{
 cout<<“Enter the number to be searched ::”;
 double x;
 cin>>x;
 int i;
 for(i=1;i<=n;i++)
 {
  if(array[i]==x)
  {
   cout<<“found at position ::”<<i<<endl;
   break;
  }
 }
 if(i>n)
  cout<<“Not found\n”;
}

int main()
{
 searching obj;
 obj.input();
 obj.linearsearch();
 return 0;
}
/********************************************************************
SAMPLE OUTPUT ::

****************************************************
This program is to implement linear search algorithm
****************************************************
Enter how many numbers you are going to enter::5
Now enter your elements ::
1.1
1.2
1.3
1.4
1.5
Enter the number to be searched ::1.5
found at position ::5
Press any key to continue

*********************************************************************/

One thought on “Linear Search”

  1. Pingback: hacked

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s