CSE2050 Spr. 2003 Exam #1, open books, open notes. Name ___________________ 1. What does the following code print? (10 pts) try { cout << 1; throw 2; cout << 3; } catch (int x) { cout << x; } catch (...) { cout << 4; } cout << 5; // ANSWER: 125 2. What is the g++ command to compile, but not link, main.cpp? (10 pts) // ANSWER: g++ -c main.cpp 3. Consider a database which associates a list of children with a parent. The data is stored in a map >, where the key is the parent's name and the value is a list of children. Write a function (using the most appropriate form of parameter passing) that prints the database. The output should be one line for each parent, in the form "parent: child child child ...". The parents should be listed in alphabetical order. The children may be printed in any order. For example: (30 pts) map > m; m["Barbara"].push_back("Sam"); m["Alice"].push_back("Phil"); m["Barbara"].push_back("John"); print(m); // Alice: Phil // Barbara: Sam John // ANSWER void print(const map >& m) { for (map >::const_iterator p=m.begin(); p!=m.end(); ++p) { cout << p->first << ":"; for (int i=0; isecond.size()); ++i) // Or use iterators cout << " " << p->second[i]; cout << "\n"; // or endl } } 4. Write a function that takes m and returns the average number of children per parent (as a double). For example, average(m) would return 1.5 (25 pts). // ANSWER double average(const map >& m) { int children=0; for (map >::const_iterator p=m.begin(); p!=m.end(); ++p) children += p->second.size(); return double(children) / double(m.size()); // beware of int division } 5. Write a function that takes m and the name of a parent and child, and returns true if that child is a child of the parent. If the parent is not listed in m, it should return false. Use both m.find() and the find() function in . For example (25 pts). ischildof(m, "Barbara", "Sam"); // true ischildof(m, "Sally", "Phil"); // false // ANSWER bool ischildof(const map >& m, const string& parent, const string& child) { map >::const_iterator p = m.find(parent); return p != m.end() && find(p->second.begin(), p->second.end(), child) != p->second.end(); }