CSE2050 Spring 2004 Exam #1. Open book, open notes. Name ________________ 1. Suppose s is a string. Write code to create a vector v with the same contents (10 pts). // ANSWER vector v(s.begin(), s.end()); // OR vector v; for (int i=0; i v(s.size()); copy(s.begin(), s.end(), v.begin()); 2. Write a function print(v) to print v one character at a time using iterators. Use the most appropriate form of parameter passing. (20 pts). // ANSWER void print(const vector& v) { for (vector::const_iterator p=v.begin(); p!=v.end(); ++p) cout << *p; } 3. Write the header file p.h given p1.cpp and p2.cpp below. Show the command to compile using g++ (30 pts). // p1.cpp #include "p.h" int main() { Person sam = makePerson("Sam", 24); cout << sam.name << " is " << sam.age << " years old\n"; // Sam is 24 return 0; } // p2.cpp #include "p.h" Person makePerson(const string& name, int age) { Person result; result.name = name; result.age = age; return result; } // ANSWER - p.h #include // for cout in p1.cpp #include using namespace std; // for cout in p1.cpp struct Person { string name; int age; }; Person makePerson(const string&, int); // Parameter names are optional // To compile: g++ p1.cpp p2.cpp 4. Write a program to read a list of integers from standard input and write them in sorted order, for example: (40 pts). ./a.out 8 -3 9 8 20 10 ^D -3 8 8 9 10 20 // ANSWER #include #include #include // for sort() using namespace std; int main() { vector v; int i; while (cin >> i) v.push_back(i); sort(v.begin(), v.end()); // or write your own sort code for (i=0; i