C++ Assignment

Due April 10



Please email your assignment to the TA, guria at cs.nyu.edu.

To compile your C++ code, Consider the following definition of a C++ class that acts like an array with bounds-checking (i.e. ensuring that a valid array index is used each time an array element is referenced).
class c_array { 
  public:
     c_array(int s): size(s) { a = new int[size]; }
     int &operator[](int i);
     int get_size() { return size; }
  private:
     int size;
     int *a;
};
Complete the definition of the c_array class by defining its operator[] method. Be sure that operator[] checks that the index is between 0 and size - 1.

Convert the c_array class to a class template, where the type of the elements of the array is a template parameter.

Declare a class template called comparable_c_array, derived from c_array, which implements the < operator for comparing two comparable_c_array's. Given comparable_c_array's X and Y, X < Y if the sum of the elements of X is less than the sum of the elements of Y.

Here is the code for a C++ function sort() for sorting arrays of integers. Convert this function to a template function that can be used to sort an array of any type supporting the < operator. Test your template function by sorting an array of floats.

Finally, create a c_array A containing elements that are themselves comparable_c_array's of integers. Then, use your sort template to,
For example, if A is a c_array of comparable_c_array's such that:
  A[0] = {2,4,1}
  A[1] = {5,3,7}
  A[2] = {0,3,2}

then after all the sorting, A will look like:
  A[0] = {0,2,3}
  A[1] = {1,2,4}
  A[2] = {3,5,7}


What to turn in:

Turn in a single C++ source file with: