CS201 – Spring 2003
Prof. Hartman
Homework #10
Due Monday, May 5th
by 5:00 pm
1.
Write a recursive function power(base, exponent) that calculates baseexponent.
For example, power(2,5) =
2*2*2*2*2 = 32. Assume exponent
is an integer greater than or equal to one, and make base of type double. (Hint: the recursion step will
use the relationship
baseexponent = base(exponent – 1)* base. The base case
would be when exponent is
one.) As usual, test your code.
2. Written:
c) Given the snippet of code:
char *message = "Howdy!";
what is output to the screen by the following commands?
i. cout << *(message + 3) << endl;
ii. cout << message + 3 << endl;
iii. cout << (*message) + 3 << endl;
d) Given the snippet of code:
int Grog[ ] = {0, 2, 4, 6, 8};
what is output to the screen by the following commands?
i. cout << *(Grog + 3) << endl;
ii. cout << Grog + 3 << endl;
3. Write a class to hold sequences of doubles. Your class should use dynamic memory to allocate an array to hold the doubles, and must include the “big three” member functions (as discussed in chapter 12). Also provide (at least) the following member functions:
o operator<< for output
o double at(int n) to access the nth element of the sequence
o void change(int n, double newval) to change the nth item to newval.
o A constructor that takes a (normal) array of doubles and a size parameter.
o
A default constructor which creates a sequence of 50
zeros.
Write a driver program for your class that tests all of the functions you’ve written. As usual, provide separate interface (.h) and implementation (.cpp) files and be careful to put const wherever it should go. You should try to write this class without using any code from the book, but if you get stuck, consider modeling your code after the StringVar class in chapter 12. It is extremely similar.