### Binary Search Problem

I am trying to get my binary search to search a sorted array for a key given by the user, but I am not sure how to implement that. This is all I have so far.
I want the program to say yes the key was found or not it was not found. Thanks in advance. No code is necessary I just want to know what to do next.
/* Example output of what I currently have.
Enter size of list (between 1 and 25): 3
Enter one list element: 12
Enter one list element: 5
Enter one list element: 7
Content of list: 12 5 7
Content of list: 5 7 12
Enter a key to search for:
5
//program ends
/*
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394`` ``````#include using namespace std; //function declaration void printArray(int arr[], int size); int insertion_sort(int arr[], int n); //int binaryS(int arr[], int key); // Driver program to test above functions int main(){ int key; int size; cout<<"Enter size of list (between 1 and 25): "; cin>>size; if (size <= 0 || size > 25)//if user's input is less then zero, an error is given { cout<<"ERROR: you entered an incorrect value for the array size!"<>arr[i]; } cout<<"Content of list: "; printArray(arr, size); int temp = insertion_sort(arr, size); //calling sorting algo cout<<"Content of list: "; printArray(arr, size); cout << "Enter a key to search for: " << endl; cin >> key; //binaryS(arr,size,key); } /* } binaryS(*arr, size, key); } int binaryS(int arr[], int key, int left, int right) { while (left <= right) { int middle = (left + right) / 2; if (arr[middle] == key) return middle; else if (arr[middle] > key) right = middle - 1; else left = middle + 1; } return -1; } */ int insertion_sort (int arr[], int length){ int j,temp; for (int i = 0; i < length; i++){ j = i; while (j > 0 && arr[j] < arr[j-1]){ temp = arr[j]; arr[j] = arr[j-1]; arr[j-1] = temp; j--; } } return 0; } /* Function to print an array */ void printArray(int arr[], int size){ int i; for (i=0; i < size; i++) printf("%d ", arr[i]); printf("\n"); } //} ``````
Last edited on