binary search

Iv been trying to write code for a binary search program but the below code keeps outputting 4 for reason... Any help would be appreciated that thanks in advance

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
 #include<iostream>
using namespace std;
int binsearch(int aray[],int size,int value);
        
int main(){
int ray[9] ={1,2,3,4,5,6,7,8,9}; 
int i,x;
cout<<"{"<<ray[0]<<",";
for(i=1; i<8; i++){
cout<<ray[i]<<",";
}
cout<<ray[8]<<"}";
    
 cout<<"whats the number?";
    cin>>x;
    
cout<<binsearch(ray,9,x);
}

int binsearch(int aray[],int size,int value){
int low,mid,high;
low=0;
high=size-1;
mid=(low+high)/2;
while(low<=high){
        mid=(low+high)/2;
   if(value=aray[mid]){
            return mid;                     ;
        }
else if(value<aray[mid]){
        high=mid-1;
    }
            else{low=mid+1;}
} 
}
You used assignment in line 27 instead of equal to .

if(value=aray[mid]) replace = with == .
Topic archived. No new replies allowed.