Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int main()
- {
- int size , temp , i , j ;
- cout << "Enter the amount of numbers you want to enter : " ;
- cin >> size ;
- int array[size];
- int key , mid , first = 0 , last = size-1 , found = 0 , index ;
- for (i = 0 ; i < size ; i++)
- {
- cout << "Enter a number : " ;
- cin >> array[i];
- }
- for (i = 0 ; i < size ; i++)
- {
- for (j = 0 ; j < size-i-1 ; j++)
- {
- if (array[j] > array[j+1])
- {
- temp = array[j];
- array[j] = array[j+1];
- array[j+1] = temp;
- }
- }
- }
- cout << "Enter the number you're looking for : " ;
- cin >> key ;
- while (first <= last && found == 0/*!found*/)
- {
- mid = (first+last)/2;
- if (key == array[mid])
- {
- found = 1;
- index = mid;
- } else if (key > array[mid]) {
- first = mid+1;
- } else {
- last = mid-1;
- }
- }
- if (found == 1)
- {
- cout << "We found the number you're looking for.\nIt's in room number." << index+1 << endl;
- } else {
- cout << "Unfortunately,we couldn't find the number for you." << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement