-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
62 lines (57 loc) · 1.31 KB
/
Copy pathbinary_search.cpp
File metadata and controls
62 lines (57 loc) · 1.31 KB
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
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
using namespace std;
void binarysearch(int marks[],int id[],int low,int high,int num)
{
int m;
int c =0;
m=(low+high)/2;
if(marks[high]<num)
{
cout<<"\n Not there";
}
while(low<=high)
{
if(marks[m]==num)
{
c++;
break;
}
else if(marks[m]>num)
{
high = m-1;
m=(low+high)/2;
}
else if(marks[m]<num)
{
low=m+1;
m=(high+low)/2;
}
}
if(c==1)
{
cout<<endl<<"\n The id number having the marks you searched for is "<<id[m];
}
else
cout<<"\n Not there";
}
int main()
{
int id[10];
int mar[10];
int a,n;
int num;
cout<<"Enter the number of students";
cin>>n;
for(a=0;a<n;a++)
{
cout<<"\n Student number "<<a+1;
cout<<"\n Enter the id";
cin>> id[a];
cout<<"\n Enter the marks";
cin>>mar[a];
}
cout<<"\n Enter the marks you want to search for";
cin>>num;
int l = 0;
binarysearch(mar,id,l,n-1,num);
}