C++ program to implement Linear search.
C++ program to implement linear search
#include<iostream>
using namespace std;
class search_list
{
int data[20];
int n;
public:
void get_data();
void linear_search();
};
void search_list::get_data()
{
cout<<"How many elements two be entered ";
cin>>n;
cout<<"Enter the elements ";
for(int i=0;i<n;i++)
cin>>data[i];
}
void search_list::linear_search()
{
int item,i;
cout<<"Enter the element to be searched ";
cin>>item;
for(i=0;i<n;i++)
{
if(data[i]==item)
{
cout<<"Element is present at "<<i+1<<" position"<<endl;
break;
}
}
if(i==n)
cout<<"Element is not present";
}
int main()
{
search_list search;
search.get_data();
search.linear_search();
return 0;
}
using namespace std;
class search_list
{
int data[20];
int n;
public:
void get_data();
void linear_search();
};
void search_list::get_data()
{
cout<<"How many elements two be entered ";
cin>>n;
cout<<"Enter the elements ";
for(int i=0;i<n;i++)
cin>>data[i];
}
void search_list::linear_search()
{
int item,i;
cout<<"Enter the element to be searched ";
cin>>item;
for(i=0;i<n;i++)
{
if(data[i]==item)
{
cout<<"Element is present at "<<i+1<<" position"<<endl;
break;
}
}
if(i==n)
cout<<"Element is not present";
}
int main()
{
search_list search;
search.get_data();
search.linear_search();
return 0;
}
No comments:
Post a Comment