CSE2050 Exam #2. Open book, open notes 1. Suppose you are writing a program to sort a file. For each of the comments below, indicate whether it belongs in the analysis or the design (3 pts each). // a. The input file name is specified on the command line. analysis // b. The program reads lines of text into a vector of strings. design // c. Lines of text are sorted alphabetically and are not case sensitive. analysis // d. If the input file does not exist, an exception is thrown. design // e. If the input file does not exist, an error message is printed. analysis 2. The template function copy_if copies elements that satisfy a predicate f() from the sequence [b, e) to the sequence starting at d. It returns an iterator pointing 1 past the last copied element of the destination sequence. template It2 copy_if(It1 b, It1 e, It2 d, Pred f) { while (b != e) { if (f(*b)) *d++ = *b; ++b; } return d; } a. (5 pts) What types of iterators satisfy It1? (circle all that apply) input forward bidirectional random b. (5 pts) What types of iterators satisfy It2? output forward bidirectional random c. (10 pts) Write a predicate even(x) which returns true if x (an int) is even and false if odd. bool even(int x) { return x % 2 == 0; } d. (10 pts) Use copy_if and a back_inserter to write a statement that copies the even elements of the array a into the empty vector v. int a[] = {3, 6, 4, 1, 8}; vector v; copy_if(a, a+5, back_inserter(v), even); e. (4 pts each) After copying, show what the following statements print. Note: v contains (6, 4, 8) and v.size() is 3 cout << *(a + 2); 4 (a[2]) cout << *v.begin(); 6 (v[0]) cout << v.end() - v.begin(); 3 (v.size()) cout << &a[3] - a; 3 (a+3 - a) int *p = a + 2; cout << ++*--p; 7 (decrement p to a+1, then increment a[1]) 3. (35 pts). A class Point represents a pair of integer values. Points can be created, printed, and tested for equality as illustrated below. Write class Point and its members. Use private/public, const member functions, constructor initialization lists, and proper parameter passing techniques as appropriate. Point a(3, 4), b(5, 6), c(5, 6); if (a.equals(b) || b.equals(c)) // false || true a.print(); // Equivalent to: cout << "(3, 4)" << endl; class Point { private: int x, y; public: Point(int a, int b): x(a), y(b) {} bool equals(const Point& p) const { return x==p.x && y==p.y; } void print() const { cout << "(" << x << ", " << y << ")" << endl; } };