CS 2005, B Term 1999
Data Structures and Programming Techniques
Solutions to the Practice Problems for Test 1

Your Name: _____________________________________

Your Lab Section: ______________     Your Login Name:_________________

Instructions.   Read each problem carefully. Write your solutions in ink in the spaces provided. Write neatly and explain your answers as clearly as possible. In the next few problems, reference is made to a class named Polynomial similar to that considered in HW2 for CS 2005 in B Term 1999. Use the following incomplete documentation and definition for the Polynomial class. Observe that this incomplete definition includes private members together with specific conventions regarding their admissible values and meanings.
// File: poly.h - header file for Polynomial class (partial version)
//
// TYPEDEF AND MEMBER CONSTANTS
//   typedef ____ Coefficient
//     Polynomial::Coefficient is the data type of each coefficient
//
//   static const DEFAULT_SIZE = ____ // initial maximum degree
//     Polynomial::DEFAULT_SIZE is the maximum initial degree of a polynomial
//     higher degrees are accomodated using dynamic memory allocation
//
// CONSTRUCTORS AND DESTRUCTOR
//   Polynomial(int initial_size = DEFAULT_SIZE)
//     Postcondition: the polynomial is 0 and initially has maximum degree initial_size
//

// CONSTANT MEMBER FUNCTIONS
//   Coefficient get_coeff(int power) const
//     Precondition: power >= 0
//     Postcondition: the value returned is the coefficient of the given power
//

// FRIEND FUNCTIONS
//   friend Polynomial operator +(const Polynomial& p, const Polynomial& q)
//     Postcondition: the polynomial returned is the sum of p and q
//
//   friend Polynomial operator *(const Polynomial& p, const Polynomial& q)
//     Postcondition: the polynomial returned is the product of p and q
//



class Polynomial {
public:

   typedef int Coefficient; // data type of each coefficient
   static const int DEFAULT_SIZE = 10; // initial maximum degree
   Polynomial(int initial_size = DEFAULT_SIZE);  // CONSTRUCTOR 

   Coefficient get_coeff(int power) const; // returns the given coefficient
   int get_degree() const; // returns the degree of the polynomial
   friend Polynomial operator +(const Polynomial& p, const Polynomial& q);
   friend Polynomial operator *(const Polynomial& p, const Polynomial& q);
private:
   Coefficient *data;  // data[k] contains coefficient of k-th power
   int degree; // highest power with nonzero coefficient
   int size;   // size of currently allocated data array

};


  1. For each of the following, write a C++ program segment to accomplish the task described.
    All variables used in your program segment must be explicitly declared.

  2. The following is an implementation of the member function get_coeff for the Polynomial class. It contains no C++ syntax errors. However, for certain values of the member variables and the input parameter it behaves differently than its specification in the header file says it should.
    Polynomial::Coefficient Polynomial::get_coeff(int power) const {
          return(data[power]); 
    }
    

  3. Fill in the following implementation of an overloaded insertion operator for the Polynomial class. This should be a non-member function.
    Here's a simple solution. Fancier versions would not write the full 
    x^i part for i=0 or i=1, and would omit the "+" sign for negative 
    coefficients to avoid "+-" symbols in the output. Such things can be 
    done using standard versions of the iomanip library. See appendix F 
    of Main and Savitch for brief descriptions of selected iomanip functions.
    
    ostream& operator <<(ostream& outs, const Polynomial& p) 
       int i;
       for (i=0; i<=p.get_degree(); i++) {
          outs << " +" << p.get_coeff(i) << "x^" << i;
       }
       outs << endl;
       return outs;
    }
    

  4. Note that the above incomplete definition of the Polynomial class does not include a copy constructor or an overloaded assignment operator.

    In the following problems, reference is made to the Clock class as considered in Lab0 for CS 2005 in B Term 1999. Use the following definition of the Clock class. Observe that this definition includes two private member variables together with specific conventions regarding their admissible values and meanings.

    // File clocks.h
    //
    class Clock {
    public:
       // CONSTRUCTOR
       Clock();
          // Postcondition: clock is set to 12:00 midnight
       // MODIFICATION MEMBER FUNCTIONS
       void set_time(int hour, int minute, bool pm);
          // Precondition: hour is between 1 and 12, minute is between 0 and 59
          // Postcondition: clock is set to time hour:minute. If pm is true, 
          // then the time is between 12:00 noon and 11:59 pm, 
          // otherwise it's between 12:00 midnight and 11:59 am.
       void advance(int minutes);
          // Postcondition: clock has been moved forward by the given number of minutes,
          // or backward by the absolute value |minutes| if minutes is negative.
       // CONSTANT MEMBER FUNCTIONS
       int get_hour() const;    
          // Postcondition: returns current hour on clock
          // in 12 hour format (1 <= returned value <= 12)
       int get_minutes() const; 
          // Postcondition: returns current minute reading
          // in 60 min format (0 <= returned value <= 59)
       bool is_pm() const;     
          // Postcondition: returns true if current time is in afternoon (12pm-11:59pm)
          // returns false otherwise
    private:
       int current_hour;        
          // in 24 hour format (0 <= current_hour <= 23)
          // 0 means 12:00 am (midnight)
       int current_minute;      
          // in 60 minute format (0 <= current_minute <= 59)
    };
    

  5. Assume that correct implementations of the member functions for the Clock class are available (such implementations would normally be placed in a separate file clocks.cxx). Assume also that a Clock object named clickclock has been declared. For each of the following, write a C++ program segment to accomplish the task described. Your program segments may access the Clock class only through its public member functions as defined in the header file clocks.h shown above.
    
    

  6. The following is an implementation of the member function set_time for the Clock class. It contains no C++ syntax errors. However, it contains semantical errors, that is, for certain values of the input parameters it behaves differently than its specification in the header file says it should.
    void Clock::set_time(int hour, int minute, bool pm) {
       if (!pm) current_hour = hour;
       else current_hour = hour + 12;
       current_minute = minute;
    }
    

  7. Fill in the following implementation (in C++) of a non-member function print_time that accepts a Clock parameter anyclock and writes the current time on anyclock to the console output stream in the usual 12 hour format hour:minute am/pm.
    #include <iostream>
    #include "clocks.h"
    void print_time(const Clock& anyclock) { 
       // use const reference parameter for efficiency 
       // also clarifies that anyclock isn't changed
       cout << "The time is " 
            << anyclock.get_hour() << ":";
       if (anyclock.get_minutes() < 10)     
          cout << "0" << anyclock.get_minutes(); 
       else
          cout << anyclock.get_minutes(); 
       if (anyclock.is_pm())
          cout << " pm" << endl;
       else
          cout << " am" << endl;
    }
    

  8. Circle two statements in the following program segment that cause the copy constructor for the Box class to be invoked. Assume that all function parameters shown explictly are passed by value.
    Box b, c;
    
     -----------
    ( Box d(c); )
     -----------
    
    b.fillbox();
    
     --------------
    ( printbox(b); )
     --------------
    
    c=b;