Answered You can hire a professional tutor to get the answer.
Assignment This program creates and implements a class to manipulate n-dimensional vectors. Don't try to picture these type of vectors in your brain....
Assignment
This program creates and implements a class to manipulate n-dimensional vectors. Don't try to picture these type of vectors in your brain. Just think of them as lists of numbers.
A driver program is provided for this assignment to test your implementation. You don't have to write the tests.
PurposeThe purpose of this assignment is to give you some experience in dynamic memory allocation and all that that entails. It should also give you a bit of additional experience in operator overloading.
ProgramYou will need to write a single class for this assignment, the VectorN class. You will need to implement several methods and functions assoicated with this class.
The class should be implemented as two separate files. The class definition should be placed in an appropriately named header (.h) file. The implementations of the class methods and any other associated functions should be placed in a separate .cpp file for the class.
class VectorNData members
The VectorN class should contain the following private data members:
- a pointer to a double. I'll refer to this data member as the vector array pointer. It will be used to dynamically allocate an array of double (the vector array).
- an unsigned integer or size_t variable used to keep track of the number of elements in the vector array. I'll refer to this data member as the vector capacity.
- (The vector array's elements will always be completely filled with values, which means there is no need to track a separate vector size. The vector size will always be the same as the vector capacity.)
Methods and associated functions
The VectorN class should have the following methods (most of which are quite small):
- VectorN::VectorN()
- The default constructor for the VectorN class takes no arguments and should initialize a new VectorN object to an empty vector array with a capacity of 0. The required logic is:
- Set the vector capacity for the new object to 0.
- Set the vector array pointer for the new object to the special value nullptr.
- VectorN::VectorN(const double values[], size_t n)
- This constructor for the VectorN class should initialize a new VectorN object to the values stored in the array values. The required logic is:
- Set the vector capacity for the new object to n.
- If the vector capacity is 0, set the vector array pointer for the new object to nullptr. Otherwise, use the vector array pointer for the new object to allocate an array of double. The number of elements in the new string array should be equal to the vector capacity.
- Copy the elements of the array values into the vector array.
- VectorN::VectorN(const VectorN& other)
- This "copy constructor" for the VectorN class should initialize a new VectorN object to the same capacity and array contents as the existing VectorN object other. The required logic is:
- Set the vector capacity for the new object to the vector capacity of other.
- If the vector capacity is 0, set the vector array pointer for the new object to nullptr. Otherwise, use the vector array pointer for the new object to allocate an array of double. The number of elements in the new vector array should be equal to the vector capacity.
- Copy the contents of the vector array of other into the vector array of the new object. If other has a vector capacity of 0, this loop will exit immediately.
- VectorN::~VectorN()
- The destructor for the VectorN class can simply call the clear() method described below.
- VectorN& VectorN::operator=(const VectorN& other)
- This overloaded copy assignment operator should assign one VectorN object (the object other) to another (the object that called the method, which is pointed to by this). The required logic is:
- Check for self-assignment. If the address stored in the pointer this is the same as the address of the object other, then skip to the final step.
- Delete the vector array for the object pointed to by this.
- Set the vector capacity for the object pointed to by this to the vector capacity of other.
- If the vector capacity is 0, set the vector array pointer for the object pointed to by this to nullptr. Otherwise, use the vector array pointer to allocate an array of double. The number of elements in the new vector array should be equal to the vector capacity.
- Copy the contents of the vector array of other into the vector array of the object pointed to by this.
- Return *this.
- void VectorN::clear()
- This method should properly set the instance back to a vector of zero elements. Delete the vector array, set the vector array pointer to nullptr, and set the vector capacity to 0.
- size_t VectorN::size() const
- Returns the size of the vector, which is equal to the vector capacity.
- operator+
- The addition operator should be overloaded to take two VectorNs and return a VectorN. The components of the result are computed by simply adding the components of the operands. For example (1, 2, 3) + (4, 5, 6) should have a result of (5, 7, 9). The operands should not be altered.
- If the two operands are of different capacities, then only the first n components of each vector should be used in the product where n is the capacity of the smaller of the two vectors. For example, (1, 2, 3) + (4, 5) should have a result of (5, 7).
- Implementation Hint: In any of the arithmetic operators, if you use a local VectorN variable to hold the result, it may be necessary to first create an empty VectorN and then directly manipulate the vector array pointer and capacity to allocate memory for the vector array.
- operator-
- The binary subtraction operator should also be overloaded to take two VectorNs and return a VectorN. The result is the component-wise difference of the operands. For example (1, 2, 3) - (4, 6, 8) should have a result of (-3, -4, -5). As with addition, the result should have the capacity of the smaller operand.
- operator*
- The binary multiplication operator should be overloaded three times. The first form, called the scalar product takes two VectorNs and produces a single double value. The scalar product is computed by multiplying the corresponding components of the two vectors and adding the results. For example the scalar product of (1, 2, 3) and (4, 5, 6) is (1 · 4) + (2 · 5) + (3 · 6) which equals 4 + 10 + 18 which gives a final result of 32.
- If the two operands are of different capacities, then only the first n components of each vector should be used in the product where n is the capacity of the smaller of the two vectors.
- The other two overloaded multiplication operators allow multiplication of a VectorN with a double constant. For example, multiplying the vector (1, 2, 3) by 3 results in the vector (3, 6, 9). Two overloaded operators are needed here, one to handle multiplication of the vector by the constant in that order, the other to handle multiplication of the constant by the vector in that order. The results should be the same in both cases.
- operator<<
- The output operator should be overloaded so that a VectorN can be sent to the standard output. An empty vector (capacity 0) should be printed as ().
- operator[]
- The subscript operator should be overloaded to provide accessor methods for the class. The provided subscript indicates which value should be accessed from the dynamically-allocated array. For speed, no error checking needs to be done.
- Don't forget that this operator needs to be overloaded twice, once as a const (read-only) method and once as a non-const (read-write) method.
- operator==
- The equality operator should be overloaded to compare two VectorNs. The two vectors are considered equal only if they are componentwise equal. For example, (1, 2, 3) is equal to (1, 2, 3), but not to (4, 3, 2). All components must be equal. If the operands have different capacities, then the vectors are automatically not equal, regardless of the component values.
A driver program, assign5.cpp is provided for this assignment. The purpose of a driver program is to test other pieces that you code. You do not need to write the driver program yourself. A copy of the driver program can also be found on turing at /home/turing/t90kjm1/CS241/Code/Spring2019/Assign5/assign5.cpp.
/** * * CSCI 241 Assignment 5 * * Author: your name * z-ID: your z-ID * Date: due date of assignment * * This program tests the functionality of the VectorN class. */ using std::cout;using std::endl;using std::out_of_range; main(){ test = ;cout << << test++ << << endl; VectorN v1;cout << << v1 << endl;cout << << test++ << << endl; ar2[] = {, , }; VectorN v2(ar2, );cout << << v2 << endl; cout << << test++ << << endl;VectorN v3(ar2, ); cout << << v3 << << v3.size() << endl;v3.clear();cout << << v3 << ;cout << v3.size() << endl;cout << << test++ << << endl; ar3[] = {, , , }; VectorN v4(ar3, );cout << << v4 << endl;cout << << v4[] << << v4[] << endl;cout << << v4[] << << v4[] << endl; VectorN v5(ar3, );v5[] = ; v5[] = ; v5[] = ; v5[] = ;cout << << v5 << endl; cout << << test++ << << endl; VectorN v6(ar2, ); VectorN v7 = v6;cout << << v6 << << v6.size() << endl;cout << << v7 << << v7.size() << endl;VectorN v8(ar3, );VectorN v9 = v8;cout << << v8 << << v8.size() << endl;cout << << v9 << << v9.size() << endl;v8[] = ;cout << << endl;cout << << v8 << << v8.size() << endl;cout << << v9 << << v9.size() << endl;cout << << test++ << << endl;VectorN v10(ar3, );VectorN v11;cout << << v10 << endl;cout << << v11 << endl;v11 = v10;v10[] = ;cout << << v10 << endl;cout << << v11 << endl;cout << endl;VectorN v12, v13;cout << << v11 << endl;cout << << v12 << endl;cout << << v13 << endl;v13 = v12 = v11;cout << << v11 << endl;cout << << v12 << endl;cout << << v13 << endl;cout << endl;v13 = v13;VectorN v14(ar2, );cout << << v13 << endl; cout << << test++ << << endl; ar4[] = {, , }; ar5[] = {, , , }; VectorN v15(ar2, ), v16(ar4, ); VectorN v17(ar5, );cout << << v13 << endl;cout << << v15 << endl;cout << << v16 << endl;cout << << v17 << endl;cout << endl;cout << << v15 + v16 << endl;cout << << v13 + v17 << endl;cout << << v15 + v13 << endl;cout << << v17 + v16 << endl;cout << endl;cout << << v15 - v16 << endl;cout << << v13 - v17 << endl;cout << << v15 - v13 << endl;cout << << v17 - v16 << endl; cout << << test++ << << endl;cout << << v15 << << v16 << ;cout << v15 * v16 << endl;cout << << v13 << << v17 << ;cout << v13 * v17 << endl;cout << << v13 << << v15 << ;cout << v13 * v15 << endl;cout << << v16 << << v17 << ;cout << v16 * v17 << endl; cout << << test++ << << endl; k = ;cout << v17 << << k << << v17 * k << endl;cout << k << << v17 << << k * v17 << endl; cout << << test++ << << endl; ar6[] = {, , }; ar7[] = {, , }; ar8[] = {, , , }; ar9[] = {, , , }; VectorN v18(ar6, ), v19(ar7, ); VectorN v20(ar8, ), v21(ar9, );cout << v18 << << v18 << ; (v18 == v18)cout << << endl;cout << << endl; cout << v18 << << v19 << ; (v18 == v19)cout << << endl;cout << << endl; cout << v18 << << v20 << ; (v18 == v20)cout << << endl;cout << << endl; cout << v21 << << v19 << ; (v21 == v19)cout << << endl;cout << << endl;/** Extra Credit: Uncomment the following lines if you choose to do the extra credit portion of this assignment.*cout << "nTest " << test++ << ": Read-write form of at() methodn" << endl;VectorN v22(ar9, 4);const VectorN v23(ar9, 4);try{v22.at(0) = 9.1;v22.at(1) = 3.97;v22.at(-1) = -7.43;}catch (out_of_range orex){cout << "Caught "<< orex.what() << endl;}cout << "v22: " << v22 << endl;cout << "nTest " << test++ << ": Read-only form of at() methodn" << endl;try{cout << "v23: (";for (unsigned i = 0; i <= v23.size(); i++)cout << v23.at(i) << ", ";cout << ")nn";}catch (out_of_range orex){cout << endl << "Caught "<< orex.what() << endl;}*/ ;} out_of_range();- Otherwise, the method should return element sub of the vector array.
- To make use of the existing standard exception class out_of_range, you will need to #include <stdexcept>; and code a using declaration for the class name. If you're confused about how to do this, take a look at the driver program.
- double& VectorN::at(int sub)
- This method is a variant of the read-write form of operator[] that provides some error checking.
- If sub is less than 0 or greater than or equal to the capacity of the vector array, this method should throw an out_of_range exception. Otherwise, it should return element sub of the vector array.
Output from the correctly functioning driver program (including the extra credit output) should look like the following:
: Default constructor printingv1: ()Test : Array constructor printingv2: (, , )Test : Clear sizeThe size v3: (, , ) After clearing, the size v3: () Test : Subscriptingv4: (, , , )v4[]: v4[]: v4[]: v4[]: v5: (, , , )Test : Copy constructorv6: (, , ) size: v7: (, , ) size: v8: (, , , ) size: v9: (, , , ) size: Changing...v8: (, , , ) size: v9: (, , , ) size: Test : Assignment operatorv10: (, , , )v11: ()v10: (, , , )v11: (, , , )v11: (, , , )v12: ()v13: ()v11: (, , , )v12: (, , , )v13: (, , , )v13: (, , , )Test : Addition subtractionv13: (, , , )v15: (, , )v16: (, , )v17: (, , , )v15 + v16 (, , )v13 + v17 (, , , )v15 + v13 (, , )v17 + v16 (, , )v15 - v16 (, , )v13 - v17 (, , , )v15 - v13 (, , )v17 - v16 (, , )Test : Vector multiplicationThe scalar product (, , ) (, , ) The scalar product (, , , ) (, , , ) The scalar product (, , , ) (, , ) The scalar product (, , ) (, , , ) Test : Scalar multiplication(, , , ) * = (, , , ) * (, , , ) = (, , , )Test : Equality(, , ) (, , ) are equal(, , ) (, , ) are equal(, , ) (, , , ) are equal(, , , ) (, , ) are equalTest : Read-write form at() methodCaught subscript out rangev22: (, , , )Test : Read-only form at() methodv23: (, , , ,Caught subscript out rangeOther Points- Implement this similarly to the last assignment. Start off at the beginning of the driver program and try to get it working piece by piece. Maintain a working program as you go.
- A makefile is required. Same as always. Make sure it has appropriate rules for all the pieces involved.
- The class should have a header file for its class definition and a source code file for its implementation. Note that the driver program assumes that your header file is called VectorN.h. If you name your header file differently, you'll need to modify the driver program accordingly.
- Programs that do not compile on turing/hopper automatically receive 0 points.
- Submit your program using the electronic submission guidelines posted on the course web site.