#include <iostream>
using namespace std;

const int LIMIT = 200;

void bubbleSort(int[], int);
int binarySearch(int[], int, int);

int inputArraySize(int); 
void inputArray(int[], int);



int main() {

	int size = inputArraySize(LIMIT);
	int array[LIMIT];
	inputArray(array, size);

	int element;
	cout << "Input a number: ";
	cin >> element;

	int index = binarySearch(array, size, element);
	if(index > -1) {
		cout << "array[" << index << "] = " << array[index] << endl;
	} else {
		cout << "The element is not in the array." << endl;
	}

	return 0;	
}

int inputArraySize(int limit) {
	
	int size;
	do {
		cout << "Input number of elements [1, " << limit << "]: ";
		cin >> size;
	} while(size < 1 || size > limit);
	return size;
}

void inputArray(int array[], int size) {

	cout << "Input all the " << size << " elements:" << endl;
	for(int i = 0; i < size; ++i) {
		cin >> array[i];
	}

}

int binarySearch(int array[], int size, int element) {
	int beg = 0, end = size;
	
	while(size - beg >= 1) {
		int mid = (beg + end) / 2;
		if(array[mid] == element) {
			return mid;
		} else if(array[mid] > element) {
			end = mid;
		} else {
			beg = mid + 1;
		}
	}

	return -1;
}

void printArray(int array[], int size) {
	for(int i = 0; i < size; ++i) {
		cout << array[i] << " ";
	}
	cout << endl;
}

void bubbleSort(int array[], int size) {
		
	for(int i = size - 1; i > 0; --i) {
		// Swaping all inversed pairs of elements.
		for(int j = 0; j < i; ++j) {
			if(array[j] > array[j + 1]) {
				swap(array[j], array[j + 1]);
			}	
		}
	}
}


