-
Notifications
You must be signed in to change notification settings - Fork 0
/
MIDTERM.CPP
50 lines (50 loc) · 834 Bytes
/
MIDTERM.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class binary {
private:
int n, m[100], loc, ele;
public:
void getdata();
void search();
void display();
};
void binary::getdata() {
cout<<"Enter The Number Of Elements\n";
cin>>n;
cout<<"Enter The Elements\n";
for (int i=0; i<n; i++)
cin>>m[i];
cout<<"Enter The Element to Be Searched\n";
cin>>ele;
};
void binary::display () {
if (loc>=0) {
cout<<"Position Of "<<ele<<" is "<<loc;
}else
cout<<"Search Was Unsuccessful\n";
};
void binary::search () {
int beg, mid, end;
loc=-1;
beg=0;
end= n-1;
while (beg<=end) {
mid = (beg+end)/2;
if (ele==m[mid]) {
loc = mid;
break;
} else if (ele < m[mid]) {
end = mid-1;
}else
beg = mid+1;
};
};
void main () {
binary B;
clrscr();
B.getdata();
B.search();
B.display();
getch();
};