#include <iostream>
using namespace std;
int main()
{
int num[10] = {3, 4, 5, 6, 7, 22, 34, 44, 57, 76};
int s = 0, l = 10, mid, loc = 0;
int _input;
// run infinty time of program
while (true)
{
// take input form user
cout << "Enter a value that you want to find in array: " << endl;
cin >> _input;
// search data from array
while (s <= l)
{
mid = (s + l) / 2;
if (_input == num[mid])
{
// cout<<"Found at position: "<<mid<<endl;
loc = mid;
break;
}
else if (_input > num[mid])
{
s = ++mid;
}
else
{
l = --mid;
}
}
// print data on screen getting from array
if (num[loc] == _input)
{
cout << "Found at position: " << loc << endl;
}
else
{
cout << "Value not found in array!" << endl;
}
cout << "******************************************" << endl;
}
return 0;
}
No comments:
Post a Comment