Key to CSE390C Sample Midterm, Spring 2025 handout #4
1. Parameter Mystery. The program produces the following output.
6 he*lo 16 the-e 8
17 th*re 27 he*-o 15
5 he*lo 15 th*re 27
2. Pointer Mystery. The program produces the following output.
5 j*y 13 0xcc00 inslee[]
15 i*slee 9 0xaa00 j*y[]
9 j*y 13 0xaa00 i*slee
3. The code would be converted to prefix notation as follows:
operator=(y, operator*(2, 3));
operator=(x, operator+(y, operator*(4, 5)));
operator<<(operator<<(operator<<(cout, "x = "), x), endl);
operator<<(operator<<(operator<<(cout, "sum = "), operator+(x, y)), endl);
operator*=(y, operator-(operator*(2, y), x)
4. One possible solution appears below.
void underline(istream & in) {
string line;
while (getline(in, line)) {
if (line.empty() || line[0] != '.') {
cout << line << endl;
} else {
cout << line.substr(1) << endl;
for (int i = 1; i <= line.size() - 1; i++) {
cout << "-";
}
cout << endl;
}
}
}
5. One possible solution appears below.
void interleave(const vector & v1, const vector & v2,
vector & result) {
int pairs = min(v1.size(), v2.size());
for (int i = 0; i < pairs; i++) {
result.push_back(v1[i]);
result.push_back(v2[i]);
}
for (int i = pairs; i < v1.size(); i++) {
result.push_back(v1[i]);
}
for (int i = pairs; i < v2.size(); i++) {
result.push_back(v2[i]);
}
}
6. One possible solution appears below.
class rectangle {
public:
rectangle(double w, double h) {
width = w;
height = h;
}
string to_string() const {
ostringstream out;
out << width << "x" << height;
return out.str();
}
double area() const {
return height * width;
}
void scale(double factor) {
width *= factor;
height *= factor;
}
private:
double width, height;
};
7. One possible solution appears below.
string operator*(const string & lhs, int rhs) {
string result;
for (int i = 0; i < rhs; i++) {
result += lhs;
}
return result;
}
string operator*(int lhs, const string & rhs) {
return rhs * lhs;
}