// Oldham, Jeffrey D. // 2000 Feb 21 // CS1321 // Mathematical Vector Implemented Using Vectors // We choose to implement a mathematical vector class MVector using // the STL vector container class. // // For example, the mathematical vector (3 4.3 5) could be stored in a // vector v having three components: v[0] = 3.0, v[1] = 4.3, v[2] = 5.0. // Below is a portion of the MVector class definition. The portion // that you should implement is indicated by "ADD SOMETHING HERE" // comments. In deciding what to add, consider the following questions. // 1) What should its name be? overloaded? operator? // 2) Should it be ordinary, static, or a friend. // Feel free to define other functions as necessary. #include class MVector { public: // Our MVector's size is measured using "size_type". typedef vector::size_type size_type; // constructors // Write three constructors: // 1) taking no arguments // 2) taking one argument of the MVector's length // 3) copying another MVector // ADD SOMETHING HERE // element access: query, change double at(const size_type i) const { return v[i]; } void set(const size_type i, const double d) { v[i] = d; return; } // query and change the MVector's size size_type size(void) const { return v.size(); } void resize(const size_type sz) { v.resize(sz); return; } // Form a new MVector by adding two MVectors. // Ex: (3 4 5.4 -2.3) + (0 -1.2 12 0) = (3 2.8 17.4 -2.3) // Assume the two MVectors have the same length. // You MUST use an STL function, e.g., sort, for_each. // ADD SOMETHING HERE // Increase the current MVector by the contents of another MVector. // Ex: If v = (3 4 5.4 -2.3) and w = (0 -1.2 12 0), then after // v += w, v = (3 2.8 17.4 -2.3). // ADD SOMETHING HERE // Form a new MVector by subtracting two MVectors. // Ex: (3 4 5.4 -2.3) - (0 -1.2 12 0) = (3 5.2 -6.6 -2.3) // Assume the two MVectors have the same length. // You MUST use an STL function, e.g., sort, for_each. // ADD SOMETHING HERE // Compute the dot product of two MVectors. // Ex: (2 3) * (1 4) = 2*1 + 3*4 = 14. // Assume the two MVectors have the same length. // Hint: Use an STL function. // ADD SOMETHING HERE // Scale an MVector. I.e., multiply each vector component by a value. // Ex: 2 * (2 3) = (4 6). // You MUST use a while or for loop and iterators. // ADD SOMETHING HERE // Scale an MVector in place. I.e., multiply each vector component // by a value, modifying the current MVector. // Ex: If v = (3 4 5.4 -2.3), then after v *= 2, // v = (6 8 10.8 -4.6). // ADD SOMETHING HERE // Compare two MVectors for equality, inequality. // (1 3 2) == (1 3 2) should evaluate to "true", (1 3 2) == (0 0 0) // to "false", and (1 3 2) == (1 3) to "false". (1 3 2) != (1 3 2) // should evaluate to "false", etc. // ADD SOMETHING HERE // input and output friend istream& operator>>(istream& in, MVector & mv) { // When doing input, do not change the given parameter until you // are sure the function will be successful. For this particular // function, it does not really matter. :( MVector answer; double temp; while (in >> temp) answer.v.push_back(temp); mv.v.clear(); mv = answer; return in; } // Write a function permitting an MVector to be output using the << // operator. // ADD SOMETHING HERE private: // We implement MVectors using STL vector. vector v; // ADD SOMETHING HERE, IF NEEDED };