Lab2 PDF
Lab2 PDF
EECE 330
Lab 2 – Spring 2020
In this lab assignment we will get familiar with C++ functions and implement two variants of
binary search as functions. Then we will get familiar with C++ classes and implement a basic array
management class and a class to represent a person.
Guidelines
• This programming assignment consists of 4 problems.
? You are supposed to submit your own work. We have a zero tolerance policy for cheating.
Penalties may include one or more of the following: zero grades on programming assignments,
failing the course, disciplinary committee, Dean’s warning, and suspension .
? Lab attendance is mandatory. Violating this rule can lead to a failing grade.
The function assumes the array A is ordered. It returns the index of e in A if found between
left and right inclusive, and −1 if not found.
Binary search takes advantage of the ordered array property. It checks whether the middle
element is equal to e, if it is, then it returns the middle index. Otherwise it checks whether the e is
smaller than the middle element, if it is then it returns the result of binary search on the left part
of the array (between left and mid-1). Otherwise, if e is larger than the middle element, then it
returns the result of the binary search on the right part of the array (between mid+1 and right).
Notice that recursive functions can run into an infinite recursion if you do not provide appropriate
base cases.
If element e is not between left and right, then binarySearch returns −1.
Implement binary search recursively in a separate function and test it by calling it from function
main.
1
Problem 2. Binary Search base case modified
The version of binary search in Problem 1 keeps on dividing the array in halves until either we find
the element we are searching for or we reach an empty array when the element we are after is not
in 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 searching for or we get a subarray of size less than or equal to 4, in which case we
perform a sequential search.
Implement the function
• Declare class Array that has an array of integers storage and a number n book-keeping the
number of elements in storage.
• 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 << operator.
2
Problem 4: Person class
A person has a name, an origin country, a gender, and a year of birth.
– a default constructor,
– a constructor with arguments that initialize all Person members,
– a copy constructor,
– an assignment operator, and
– a comparator operator (bool operator <(Person & other) const) that compares the
person to the other based on alphabetical name order.
Also provide a print method void print(ostream & ostr) that takes a reference to an output
stream and prints the members of an instance of class Person.
Use the print method to support a helper streaming operator: ostream & operator << (ostream
& ostr, Person & p) print an instance of class Person.
Use your declared class and test its methods in main.