#include <iostream>
using namespace std;
int main()
{
int a[5], i, num;
int low, high, mid;
//num – Number that we want to search.
//low – Starting position of the array.
//high – Last position of the array.
//mid – Middle position of the array.
cout << "Enter 5 numbers in ascending order: ";
for(i = 0; i < 5; i++)
{
cin >> a[i];
}
cout << "Enter number to search: ";
cin >> num;
low = 0;//low = 0 means the first position of the array.
high = 4; //high = 4 means the last position of the array.
while(low <= high)
{
mid = (low + high) / 2;
//Finds the middle position of the array.
//For example, (0 + 4) / 2 = 2.
//So, the middle position is 2 (number 30).
if(a[mid] == num)
{
cout << "Number found";
return 0;
}
if(num > a[mid])
{
low = mid + 1; //If the searching number is greater than the middle number, search the right side.
mid + 1 moves the starting position to the right side.
}
else
{
high = mid - 1;//If the searching number is smaller than the middle number, search the left side. mid - 1 moves the ending position to the left side.
}
}
cout << "Number not found";
return 0;
}




