Key to CSE390C Sample Midterm, Spring 2022 handout #4 1. Parameter Mystery. The program produces the following output. elon! 3 11 musk? 18 elon!! 2 8 elon!? 16 elon!! 2 16 musk 18 2. Pointer Mystery. The program produces the following output. 6 0xaa00 *oe 13 b*den 9 0xdd00 *iden 11 **e 11 0xaa00 *oe 11 *iden 3. One possible solution appears below. void replace_all(istream & in, char target, char replacement) { string line; while (getline(in, line)) { for (char ch : line) { if (ch == target) { cout << replacement; } else { cout << ch; } } cout << endl; } } 4. One possible solution appears below. void remove_zeros(vector<int> & v) { for (int i = v.size() - 2; i >= 0; i--) { if (v[i] == 0) { for (int j = i; j < v.size() - 1; j++) { v[j] = v[j + 1]; } v[v.size() - 1] = 0; } } } 5. One possible solution appears below. class multi_string { public: multi_string(int times, const string & the_text) { count = times; text = the_text; } string to_string() const { string result; for (int i = 0; i < count; i++) { result += text; } return result; } int get_count() const { return count; } void set_count(int new_count) { count = new_count; } private: int count; string text; }; 6. One possible solution appears below. ostream & operator<<(ostream & out, const vector<int> & v) { out << "["; if (v.size() > 0) { out << v[0]; for (int i = 1; i < v.size(); i++) { out << ", " << v[i]; } } out << "]"; return out; }
Stuart Reges
Last modified: Wed Apr 27 16:51:14 PDT 2022