// Questions 4 and 8 from Test 1 // Beth Katz - October 2009 #include #include using namespace std; // do code in question 4 void question4( ); // do code in question 8 void question8( ); // returns smallest value in v (or 0 if it is empty) template T mystery(const vector & v); int main( ) { vector intV; cout << endl << "Output for question 4:" << endl; question4( ); cout << endl; cout << "The answer from question 8:" << endl; cout << "===========================" << endl; question8( ); cout << "===========================" << endl; cout << "You can't be sure about that last character." << endl; cout << "After 'delete p3;' p3 is a dangling reference" << endl; cout << "There isn't a memory leak." << endl; cout << "When p1 changed, it was pointing to b." << endl; return 0; } void question4( ) { vector a(4); vector n(3); a[0] = "horse"; a[1] = "panda"; a[2] = "fox"; a[3] = "giraffe"; n[0] = 75; n[1] = 55; n[2] = 80; cout << "Result of mystery(n): " << mystery(n) << endl; cout << "Result of mystery(a): " << mystery(a) << endl; } template T mystery(const vector & v) { if (v.size( ) <= 0) { return 0; } T value = v[0]; for (unsigned i = 1; i < v.size( ); i++) { if (v[i] < value) { value = v[i]; } } return value; } void question8( ) { char a = 'y', b = '$', *p1 = &b, *p2 = new char('w'), *p3 = new char(*p1); cout << a << b << *p1 << *p2 << *p3 << endl; b = *p1; delete p3; p1 = p2; cout << a << b << *p1 << *p2 << *p3 << endl; }