-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProva_search.cpp
More file actions
59 lines (48 loc) · 1.03 KB
/
Prova_search.cpp
File metadata and controls
59 lines (48 loc) · 1.03 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
#include<cstdio>
#include<cstdlib>
#include<fstream>
#include<math.h>
#include<iostream>
using namespace std;
const int N=100;
int search(double *Gamma, int M, double x);
int main(){
double M[N];
int i,l;
for(i=0; i<N;i++){
M[i]=(double)i/N;
}
for(i=0; i<N; i++){
cout<<"i= "<<i<<" M[i]= "<<M[i]<<endl;
}
cout<<endl<<endl<<endl;
l=search(M,N,0.2998);
cout<<endl<<"********* l="<<l<<" *********"<<endl;
return 0;
}
int search(double *Gamma, int M, double x){ //Binary search
int a,b,l,result;
bool check;
a=0;
b=M-1;
do{
l=(a+b)/2;
if(x<=Gamma[l]){
if(x>=Gamma[l-1]){result=l;
check=true;}
else{
b=l;
check=false;}
}
else{
if(x<=Gamma[l+1]){
result=l+1;
check=true;
}
else{
a=l;
check=false;}
}
}while(check==false);
return result;
}