#include <iostream>
using namespace std;
int main()
{
int arr[]={1,2,3,4,5,6,12,12,12,22};
const int size=10;
int i,j,value;
int middle=0,first=0 ,last=0,position=-1;
bool found=false;
cout <<“array is:\n”;
for(i=0;i<size-1;i++)
{
cout <<arr[i] <<“,”;
}
cout <<arr[i] <<endl;
cout <<“enter a value to search :\n”;
cin >> value;
last=size-1; // have the index of last number
first=0;
while(!found &&first<=last ) //found is bool
{
middle=(first+last)/2;
if(arr[middle]==value)
{
position=middle; // always intend to be the middle position in binary search
found=true;
}
else if(arr[middle]>value)
{
last=middle-1;
}
else
{
first=middle+1;
}
}
cout << “index of the value is ” << position <<endl;
return 0;
}