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

### (also see the self-test exercises in chapters 1-4 of the textbook)

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 int DEFAULT_SIZE = ____ // initial maximum degree
//     Polynomial::DEFAULT_SIZE is the maximum initial degree of a polynomial
//     higher degrees are accomodated using dynamic memory allocation
//
// CONSTRUCTOR
//   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.
• Dynamically allocate memory for an array called stuff containing 12 entries of type int, then release the memory.
```

```
• Assuming that p and q are objects of type Polynomial, compute the i-th coefficient of the product polynomial p*q (in terms of the coefficients of p and q) and store it in a variable named temp. Assume that i is between 0 and the lesser of the degrees of p and q. Your program segment may access the private members of Polynomial objects.
```

```

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]);
}
```
• Give an example of specific values of the input parameter power and the private member variables that are consistent with the specification in the header file but for which the above implementation of get_coeff may behave in a manner not consistent with the specification provided in the header file.
```

```
• Correct the implementation of get_coeff so that it behaves according to the specification given above in the header file poly.h.
```

```

3. Fill in the following implementation of an overloaded insertion operator for the Polynomial class. This should be a non-member function.
```ostream& operator <<(ostream& outs, const Polynomial& p)

```

4. Note that the above incomplete definition of the Polynomial class does not include a copy constructor or an overloaded assignment operator.
• Which of the following locations of the header file poly.h should contain the prototype for the copy constructor? Circle your answer.
• private part of the class definition
• public part of the class definition
• after the class definition, with the other non-member functions
• Give a full implementation for the overloaded assignment operator in the implementation file poly.cxx.
```

```

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.
// 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.
• Set the time on clickclock to 1:23 am.
```
```
• Store the current minute reading on clickclock in a variable named thisminute
(thisminute should be declared in your program segment).
```

```
• Move the time on clickclock forward by half an hour.
```
```
```
```

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;
}
```
• Give two different examples of values of the input parameter list (hour, minute, pm) for which the above implementation of set_time assigns an incorrect value to at least one of the private member variables. Incorrect means not consistent with the specification provided in the header file.

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)

```

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;

```