You are on page 1of 3

Data Structures and Analysis of Algorithms

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.

• It is due on Feb 10, 2020, 11:55 pm.

• Topics: C++, functions, classes.

• Readings: First three chapters of Stroustrup, Chapter 4 and 9.

? 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.

Problem 1. Recursive binary search


Implement recursive binary search using the following function prototype.

int binarySearch(const int A[], int left, int right, int e)

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

int binarySearch4(const int A[], int n, int x)

The function is supposed to return the index of x in A if found and −1 otherwise.


Write a program to test your function.
Test your function on large arrays. 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 << operator.

2
Problem 4: Person class
A person has a name, an origin country, a gender, and a year of birth.

• Represent the origin and the gender with enum types.

• Develop a class Person with

– 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.

You might also like