int firstOcc(int a[],int m,int x)
{
int high=m-1,low=0,mid,index=-1;
while(low<=high){
mid=(low+high)/2;
if(a[mid]<x){
mid=low+1;}
if(a[mid]>x){
mid=high-1;}
if(a[mid]==x){
index=mid;
high=mid-1;}
}
return index;
}
why is my function isn't working ?! finding first occurrence. what is wrong with it ?
can't find the bug, copied almost identical code from the internet it worked but I need to know why my code isn't working
wrong:
mid=low+1;}
…
mid=high-1;}
right:
low=mid+1;}
…
high=mid-1;}
mid
change is off the table.