首页 > > 详细

C++辅导:Assignment辅导C/C++设计

Introduction
c++,stlvector, ,constructordestructor,
tail,head, sort。main
Requirement
P a g e | 1
CMPSC 122 – Intermediate Programming
Assignment #4
Spring 2016
Due Date: Monday, March 28, 2016
Total Points: 100
Implement a generic (template) class called MyArray. A generic class is a general version of a class; it
is a homogenous data structure that handles multiple data types. MyArray will be similar to C++
arrays but will not be constrained to a fixed size. The array size will grow dynamically when the user
inserts an element to a full array. The class should not use vectors. Instead, you should use
dynamic memory allocation (a dynamic array).
The class should use three member variables:
- myarray: the dynamic array
- size: the current size of the array
- used: the number of elements currently used in the array
The class should have the following functionalities:
? A default constructor that initializes the size of the array to 5.
? A default destructor that frees the dynamic memory that was allocated.
? int length( ) a method that returns an integer value indicating the number of elements
currently used in the array.
? void insertHead(Item i) a method that inserts a new item making it the first element in the
array. If the array is full, then a new dynamic array of double the size should replace the
existing array.
? void insertTail(Item i) a method that inserts a new item making it the last element in the
array. If the array is full, then a new dynamic array of double the size should replace the
existing array.
? void deleteHead( ) deletes the first element in the array.
? void deleteTail( ) deletes the last element in the array.
? void sortAscending( ) sorts the elements of the array in ascending order. Use bubbleSort to
do that.
? void sortDescending( ) sorts the elements of the array in descending order. Use
insertionSort to do that.
? C++ allows the overloading of the [ ] operator. This feature makes it possible to access
an element in a MyArray object in an array-like behavior. Overload this operator and
make sure to have boundary checks using assertions. If the user tries to access an
element outside the range of the array or at a location that is not used yet then an error
message should be displayed on the screen.
For example, the following code:
P a g e | 2
MyArray a1;
cout << “Number of elements in a1= “ << a1.length() << endl;
a1.insertTail(35);
a1.insertTail(45);
a1.insertHead(55);
a1.deleteTail();
for(int i =0; i < a1.length(); i++)
cout << a1[i] << endl;
Will result in:
Number of elements in a1= 0
55
35
If we try any of the two instructions the result should be an assertion error:
cout << a1[4]; //Subscript. uninitialized.
cout << a1[14]; //Subscript. out of range.
Write a main( ) method that provides a menu driven interface that allows the user to test whether
each member function of myArray is working properly. The menu should provide the following
functionalities:
o Create a new array: allows the user to create an array of one of the following types: bool, char,
double, float, int, string
o Get Length: the current number of elements used in the array
o Insert a new element at the head of the array
o Insert a new element at the tail of the array
o Delete the first element
o Delete the last element
o Sort the elements in ascending order
o Sort the elements in descending order
o Print all the elements in the array
o Quit the program
What to hand in
Please submit your C++ source file electronically through Angel.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!