EECS 370
Sample Exam Question
Arrays in C/C++ of N elements have subscripts from 0 as the lower bound
to N-1 as the upper bound. Other languages (like Pascal) allow the
user to specify any lower bound and upper bound subscript values to
create an array of (upper bound - lower bound + 1) elements.
Create a class "Array" in C++ that allows a user to specify both forms
of arrays. Each element in the array will be a floating point value.
The class is to maximize the amount of information hiding. Your answer
must clearly show the information that will go into the header file
"array.h" and into the source code file "array.cpp". The header
file must guarantee that multiple copies of the header information
must not exist in any source code file.
-
Write a constructor for the class that will take one integer
parameter and possibly one floating point parameter. The integer
parameter will specify the number of elements to be dynamically created
for the array. The lower bound subscript will be 0 and the upper
bound subscript will be the integer value minus one. If a floating
point parameter is given, all elements of the array are to be initialized
with this value. If a floating parameter value is not given, all elements
of the array are to be initialized to 0.0.
- Write a constructor for the class that will take two integer
parameters and possibly one floating point parameter. The first
integer parameter will be the lower bound subscript value. The second
integer parameter will be the upper bound subscript value. The number
of elements to be dynamically created for the array is
(upper bound - lower bound + 1). The upper bound must always be greater
than or equal to the lower bound. If a floating point parameter is given,
all elements of the array are to be initialized with this value. If a
floating parameter value is not given, all elements of the array are to
be initialized to 0.0. When accessing elements from this form of an array,
the given subscript value will need to be translated into the standard
C/C++ subscript form. For example, assume the lower bound is -10 and the
upper bound is +10. This array would have 21 elements. The subscript -10
would access the 0-th ("zero-th") element of the array. The subscript -5
would access the 5-th ("fifth") element of the array. The subscript 0 would
access the 10-th ("tenth") element of the array. The subscript 10 would
access the 20-th ("twentieth") element of the array.
- Overload the [ ] operator to access an element in the array.
The accessed element must be able to be used on either the left-hand-side
or the right-hand-side of an assignment statement. The subscript specified
must be in the range from the lower bound to the upper bound. If a proper
subscript, give an error message and exit() the program.
- Write a copy constructor and destructor for this class if needed.
If they are not needed, explain why not.
- Overload the = and == operators when both the left-hand-side and
the right-hand-side are instances of the Array class. Two arrays are
equal if they contain the same number of elements and the i-th element
of both arrays are the same for all values of i (note: the allowable
subscript values may differ between the two arrays).
- Write two inline methods of the class: upper() and lower().
The method upper() is to return the upper bound subscript value, while
lower() is to return the lower bound subscript value.
- The class is NOT to have a data member to hold the number of
elements in the array, instead write the utility method size(). This
method is to return the number of elements that the array can hold
and is only be used internally in the class.
- Write the method grow(). This method is to dynamically increase
the number of elements that can be stored in the array. If an integer
parameter is given, the number of elements is to increase by this
amount. The integer parameter must be a value of 1 or larger. If a
parameter of zero or less is given, an error message should be given
and the array is not to increase. If no integer parameter is given,
the number of elements in the array is to double. When grow() is invoked,
the upper bound subscript is to be changed the reflect the new elements
added to the array. The lower bound subscript is not to be changed.
The value in the i-th element prior to grow() being invoke, must be
in the i-th element when grow() returns.