// CS201 HW10 // Problem 01 #include using namespace std; #include using namespace std; int power(int base, int exponent); int main () { int base, exp; cout<<"Enter the base number, hit return, and enter the exponent:\n"; cin>> base >> exp; cout<< "The answer is " < 0) { return base * power (base, exponent-1); } else { return 1; } } /* Enter the base number, hit return, and enter the exponent: 2 5 The answer is 32 Press any key to continue */ Problem 2: a) True b) float *bar, foo; bar = &foo; c) i) d ii) dy! iii) 75 d) i) 6 ii) a memory address Problem 3: //Doubles.h #ifndef DOUBLES_H #define DOUBLES_H #include using std::ostream; using std::istream; class Doubles { public: friend ostream &operator<<(ostream &, const Doubles & ); Doubles ( int=50); Doubles ( const Doubles &); ~Doubles(); Doubles &operator=(const Doubles &); double at(int); void change(int, double); private: double *ptr; int size; }; #endif //Doubles.cpp #include "Doubles.h" using namespace std; Doubles::Doubles( int arraySize) { size = arraySize; ptr = new double[size]; for ( int i=0; i < size; i++) ptr[i]=0.0; } Doubles::Doubles(const Doubles & ds) { size = ds.size; ptr = new double[ds.size]; for ( int i= 0; i < size; i++ ) ptr[i] = ds.ptr[i]; } Doubles & Doubles::operator =(const Doubles & ds ) { if(&ds != this ) { //check for self assignment if (size != ds.size) { delete [] ptr; size = ds.size; ptr = new double[size]; } for ( int i=0; i< size; i++) ptr[i] = ds.ptr[i]; } return *this; } Doubles::~Doubles() { delete [] ptr; } double Doubles::at(int n) { return ptr[n]; } void Doubles::change(int n, double d) { ptr[n] = d; } ostream & operator<<(ostream & out, const Doubles & ds ) { for (int i=0; i < ds.size; i++) out << ds.ptr[i] <<' '; cout << endl; return out; } //driver.cpp #include "Doubles.h" using namespace std; int main() { Doubles ds1; cout << ds1; ds1.change(5, 101.0); cout << ds1; cout << ds1.at(5) << endl; Doubles ds2(ds1), ds3; cout << ds2 << endl; ds3 = ds2; cout << ds3; return 0; } /* 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 101 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 101 0 0 0 0 0 101 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 101 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Press any key to continue */