0% found this document useful (0 votes)
71 views

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

The document describes a C++ program that implements a linear search algorithm. The program defines a class called "search" that contains functions to get data input from the user, store user-provided numbers in an array, and search the array to find a target number. The main function creates a search object, calls the data input and searching functions, and returns output indicating whether the target was found or not found.

Uploaded by

Vibhor Chander
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

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

The document describes a C++ program that implements a linear search algorithm. The program defines a class called "search" that contains functions to get data input from the user, store user-provided numbers in an array, and search the array to find a target number. The main function creates a search object, calls the data input and searching functions, and returns output indicating whether the target was found or not found.

Uploaded by

Vibhor Chander
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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