C++代写:CMPSC122 My Array

用C++代写一个关于array的generic class,属于常见的数据结构编程。

Requirement

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++ vectors. 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 have 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 elements.
  • 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 and the items in the old array are copied to the new 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 bubbleSort to do that.
  • Item getMax( ) returns the maximum value in the array.
  • Item getMin( ) returns the minimum value in the array.
  • 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:

1
2
3
4
5
6
7
8
MyArray<int> 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:

1
2
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:

  • Create a new array: allows the user to create an array of one of the following types: bool, char, double, float, int, string
  • Get Length: the current number of elements used in the array
  • Insert a new element at the head of the array
  • Insert a new element at the tail of the array
  • Delete the first element
  • Delete the last element
  • Sort the elements in ascending order
  • Sort the elements in descending order
  • Print all the elements in the array
  • Display the maximum value
  • Display the minimum value
  • Quit the program

What to hand in

Please submit your C++ source file electronically through Canvas