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

Binary

The document implements binary search using classes in C++. It defines a binary class with data members to store an array of numbers and the search element. The getdata() method takes input from the user, the numbers and search element. The search() method performs binary search on the numbers array to find the position of the search element. It returns the position if found, otherwise prints that the element is not found. The main() function creates an object, calls the getdata() and search() methods to search for the element entered by the user.

Uploaded by

Swapnil Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Binary

The document implements binary search using classes in C++. It defines a binary class with data members to store an array of numbers and the search element. The getdata() method takes input from the user, the numbers and search element. The search() method performs binary search on the numbers array to find the position of the search element. It returns the position if found, otherwise prints that the element is not found. The main() function creates an object, calls the getdata() and search() methods to search for the element entered by the user.

Uploaded by

Swapnil Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q 6. WAP to impliment binary search with classes .

#include<iostream.h>
#include<conio.h>
class binary
{
int num[20],s;
public:
void getdata();
void search();
};
void binary::getdata()
{
int i;
cout<<"Enter 10 numbers in ascending order :\n";
for(i=0;i<10;i++)
{
cin>>num[i];
}
cout<<" Enter the search element :\n";
cin>>s;
}
void binary::search()
{
int beg=0,end=9,mid;
while(beg<=end)
{
mid=(beg+end)/2;
if(s==num[mid])
{
cout<<" Found at the position : "<<mid+1;
break;
}
if(s>num[mid])
{
beg=mid+1;
}
else
{
end=mid-1;
}
}
if(beg>end)
{
cout<<" Not found ";
}
}
void main()
{
clrscr();
binary b;
b.getdata();
b.search();
getch();
}

OUTPUT 6 :

Enter 10 numbers in ascending order :


4
9
13
17
23
29
36
42
59
66
Enter the search element :
29
Found at the position : 6

You might also like