// Oldham, Jeffrey D. // 2000 Feb 21 // CS1321 // // Modified by: // Massingill, Berna L. // 2001 Feb ?? // // 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 #include // has istream #include // has assert() // ADD SOMETHING HERE, IF NEEDED 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 // ---- Functions to query and change size, elements ------------------ // Post: returns number of elements in vector. size_type size(void) const { return v.size(); } // Pre: sz >= 0. // Post: current object has sz elements; i.e., size() will return sz. void resize(const size_type sz) { v.resize(sz); return; } // For now we'll define member functions at() and set() to query // and change elements. Overloading the [] operator requires too // much trickery. // Pre: 0 <= 1 < size() // Post: returns i-th element of current object. double at(const size_type i) const { assert(i < size()); return v[i]; } // pre: 0 <= 1 < size() // post: i-th element of current object set to d. void set(const size_type i, const double d) { assert(i < size()); v[i] = d; return; } // ---- Arithmetic operations ----------------------------------------- // 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) // You may 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) // You may 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. // You may 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 // ---- Relational operations ----------------------------------------- // 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 ---------------------------------------------- // Pre: mv has desired size (say N). // Post: mv contains N doubles have been read from in. 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. MVector answer(mv.size()); for (MVector::size_type i = 0; i < mv.size(); ++i) in >> answer.v[i]; 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 };