CPSC 102-001 Computer Science II
(Advanced C Programming)

Fall 2010
09:05-09:55MW Daniel 415
Lab 9

Objectives:
To implement and exercise the vec_t object

What to hand in:
A tar.gz archive of your lab9/ directory, including:
  1. A README file containing
    1. Course id--section no
    2. Name
    3. Assignment description
    4. Brief solution description (e.g., program design, description of algorithm, etc., however appropriate).
    5. Lessons learned, identified interesting features of your program
    6. Any special usage instructions
  2. Makefile
  3. source code (.h headers and .cpp source)
  4. object code (do a make clean before tar)
How to hand in:
See sendlab notes

Description:
  1. Following the class notes, define the vec_t class in the vector.h interface, providing the following class member functions:
      // constructors (overloaded)
      vec_t(double ix=0.0, double iy=0.0, double iz=0.0);
    
      // copy constructor
      vec_t(const vec_t& rhs);
    
      // destructor (default ok)
      ~vec_t();
    
      // assignment operator
      vec_t& operator=(const vec_t& rhs);
    
      // accessor/mutator
      const double& operator[](int i) const;
      double& operator[](int i);
    
      // unary operators
      vec_t operator-();
    
      // binary operators
      vec_t operator-(const vec_t& rhs);
    
      // friend operators
      friend vec_t operator*(const vec_t& lhs, const double& s);
      friend vec_t operator*(const double& s, const vec_t& rhs);
    
      // friend i/o functions
      friend std::ostream& operator<<(std::ostream& s, const vec_t& rhs);
      friend std::ostream& operator<<(std::ostream& s, vec_t *rhs);
      friend std::istream& operator>>(std::istream& s, vec_t& rhs);
      friend std::istream& operator>>(std::istream& s, vec_t *rhs);
    
      // public member functions
      double dot(const vec_t&);
      double len();
    		

  2. The two member functions (a.k.a. methods) dot() and len() must be implemented in the vec_t implementation vector.cpp
  3. Your code should be such that the following main() routine works unaltered:
    int main()
    {
            vec_t   v1(1.0, 2.0, 3.0);
            vec_t   v2(4.0, 5.0, 6.0);
            vec_t   v3, v4, v5(v2);
      
      // use operator>> intead of fscanf
      std::cout << "Enter 3 values for v3"  << std::endl;
      std::cin >> v3;
            
      // use ostream instead of 'prn' function
      std::cout << "v1 = " << v1 << std::endl; 
    
      // test operator[]
      std::cout << "v2 = " << v2 << std::endl;
      v2[1] = 3.0;
      v2[0] = v2[2];
      std::cout << "v2 = " << v2 << std::endl;
            
      // use ostream instead of 'prn' function
      std::cout << "v3 = " << v3 << std::endl; 
    
      // test binary operator-
      v4 = v1 - v3;
      std::cout << "v4 = v1 - v3 = " << v4 << std::endl;
      
      // test operator*
      std::cout << "v4 * 5 = " << v4 * 5 << std::endl;
      
      // test global binary operator*
      std::cout << "3 * v4 = " << 3 * v4 << std::endl;
      
      // test unary operator- 
      std::cout << "-v5 = " << -v5 << std::endl;
      
      // test dot product member function
      std::cout << "v3 . v4 = " << v3.dot(v4) << std::endl;
      
      // test len product member function
      std::cout << "||v4|| = " << v4.len() << std::endl;
    }
    		
  4. Sample input (entered by user):
    7 8 9
    		
  5. Sample output (bold text indicates user input):
    Enter 3 values for v3
    7 8 9
    v1 = 1 2 3
    v2 = 4 5 6
    v2 = 6 3 6
    v3 = 7 8 9
    v4 = v1 - v3 = -6 -6 -6
    v4 * 5 = -30 -30 -30
    3 * v4 = -18 -18 -18
    -v5 = -4 -5 -6
    v3 . v4 = -144
    ||v4|| = 10.3923