Key to CSE390C Midterm, Winter 2024 handout #5 1. Parameter Mystery. The program produces the following output. #uke 40 (solo) 43 30 #solo) 60 (luke) 63 30 (luke) 30 (solo) 60 60 2. Pointer Mystery. The program produces the following output. 0xdd00 0xcc00 17 vader vader-vader 0xee00 0xcc00 24 vader vader-vader 0xdd00 0xcc00 24 vader-vader vader-vader?? 3. The code would be converted to prefix notation as follows: operator=(x, operator-(5, 2)) operator=(y, operator*(4, operator+(x, 3))) operator<<(operator<<(operator<<(cout, "x = "), x), endl) operator=(y, operator-=(x, operator+(2, operator/(14, 3)))) operator<<(operator<<(operator<<(operator<<(cout, x), " "), y), endl) 4. One possible solution appears below. string line1; while (getline(input, line1)) { if (line1 =="name:") { string line2; getline(input, line2); int comma_index = line2.find(","); string last = line2.substr(0, comma_index); string first = line2.substr(comma_index + 2); cout << first << " " << last << endl; } else { cout << line1 << endl; } } } 5. One possible solution appears below. bool is_pairwise_sorted(const vector<int> & list) { for (int i = 1; i < list.size(); i += 2) { if (list[i] < list[i - 1]) { return false; } } return true; } 6. One possible solution appears below. class complex { public: complex(double init_a = 0, double init_b = 0) { a = init_a; b = init_b; } void scale(double factor) { a *= factor; b *= factor; } double abs() const { return sqrt(a * a + b * b); } string to_string() const { ostringstream out; out << a; if (b != 0) { out << " + " << b << "i"; } return out.str(); } private: double a, b; }; 7. One possible solution appears below. complex & operator-=(const complex & rhs) { a -= rhs.a; b -= rhs.b; return *this; }
Stuart Reges
Last modified: Tue Feb 13 13:38:55 PST 2024