#include <iostream>
usingnamespace std;
int main()
{
constint arraySize = 5;
int target;
// Array size and values already known.
int array[arraySize] = {1, 2, 3, 4, 5};
int first, mid, last;
cout << "Enter a target to be found: ";
cin >> target;
// Initialize first and last variables.
first = 0;
last = 2;
while(first <= last)
{
mid = (first + last)/2;
if(target > array[mid])
{
first = mid + 1;
}
elseif(target < array[mid])
{
last = mid + 1;
}
else
{
first = last + 1;
}
}
// When dividing can no longer proceed you are left with the
// middle term. If the target equal that term you are
// successful.
if(target == array[mid])
{
cout << "Target found." << endl;
}
else
{
cout << "Target not found." << endl;
}
return 0;
}