Question: undefined Problem 2. Binary Search, base case modified The version of binary search in Problem 1 keeps on dividing the array in halves until we
undefined
Problem 2. Binary Search, base case modified The version of binary search in Problem 1 keeps on dividing the array in halves until we either find the element we are searching for or we reach an empty array when said element is missing from the array. Modify the base case of binary search by performing a sequential search when the subarray size becomes less than or equal to 4. That is, we keep on dividing in halves until either we find the element we are after or we get a subarray of size less than or equal to 4; in which case we perform a sequential search. Implement the function int binarySearch4(const int A[], int n, int x) The function is supposed to return the index of 1 in A if found and -1 otherwise. Test it on the array of odd numbers 1,3,5,7,9,...,199,201 and use the function to search for the numbers -2, 2, 13, 20, 55, 99, 157, 180, 183, 199, 200, 250. Problem 3. Array class Consider a class Array that manages a C++ array of integers. It has enough storage for an array of up to 1024 integers. It starts with zero elements using the storage. It provides a method bool addElement(int e) that adds elements to the array. In case the number of elements in the array is less that the full capacity (1024), then the method places the new element e at the first unused index of the storage starting from index 0 and returns true. In case the number of elements in the array is equal to the full capacity, the method prints an error and returns false. Declare class Array that has an array of integers storage and a number n book-keeping the number of elements in storage. Provide a default constructor for the array that initializes n to zero. Provide method addElement(int e) that adds elements e to the array. Provide a copy constructor Array(Array & a) that takes a reference to an existing array a and copies its content a.storage and a.n to the constructed object. Provide an assignment operator Array & operator = (Array & oa) that takes a reference to an existing array oa and copies its content oa.storage and oa.n to the object. Once done it returns a reference to the this object. (return *this;). Provide a helper function ostream& operator (ostream& ostr, Array & arr) that helps print the content of an instance arr of class Array. Test your code by declaring two instances of type Array in main, adding elements to them using addElement, copying one to the other using the copy constructor and the assignment operator and printing them to cout using the
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
