Practical-2: AIM:-Write A Program in C++ To Implement Linear Search. Source Code

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

2312053

C++ WITH DATA STRUCTURES LAB

PRACTICAL-2
AIM:-Write a program in C++ to implement linear search.
SOURCE CODE:-
#include<iostream>
using namespace std;
class search // class of name linear search is created
{
int i,n,item,f=0; // we take 4 integer variables
int a[10];
public:
void gdata(); // a function to get data from the user
void searching(); // a function used for searching
};
void search::gdata() // defining the gdata function
{
cout<<"Enter number of elements ";
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
}
void search::searching() // defining the searching function
{
cout<<"enter the item to be search"<<endl;
cin>>item;
for(i=0;i<n;i++)
{
if(a[i]==item)
{
cout<<"The element is found at location"<<i+1<<endl;
f=1;
}
}
if(f==0)
{
cout<<"element is not found";
}
}
int main()
{
search L;
2312053
C++ WITH DATA STRUCTURES LAB

L.gdata();
L.searching();
return 0;
}
OUTPUT :-

You might also like