CSE390C Sample Midterm handout #3 Winter 2023 1. Parameter Mystery (15 points). Consider the following program: #include <iostream> #include <string> using namespace std; void mystery(int a, string & b, int & c, string d, int & e) { a++; b[2] = '*'; c = a + 10; d[3] = '-'; e--; cout << a << " " << b << " " << c << " " << d << " " << e << endl; } int main() { int a = 5; string b = "hello"; int c = 7; string d = "there"; int e = 9; mystery(a, b, c, d, e); mystery(c, d, e, b, c); cout << a << " " << b << " " << c << " " << d << " " << e << endl; return 0; } List below the output produced by this program. 2. Pointer Mystery (15 points). Consider the following program: #include <iostream> #include <string> using namespace std; void mystery(int a, string & b, int & c, int * d, string e) { a = a + 2; b[1] = '*'; c = c + 4; d = &c; *d = c + 2; e += "[]"; cout << a << " " << b << " " << c << " " << d << " " << e << endl; } int main() { int a = 3; string b = "jay"; int c = 7; int * d = &a; string e = "inslee"; mystery(a, b, c, d, e); mystery(c, e, a, &a, b); cout << a << " " << b << " " << c << " " << d << " " << e << endl; return 0; } For the purposes of this problem, assume that the variables in main are stored at the following memory addresses: main's a variable is stored at address 0xaa00 main's b variable is stored at address 0xbb00 main's c variable is stored at address 0xcc00 main's d variable is stored at address 0xdd00 main's e variable is stored at address 0xee00 List below the output produced by this program. 3. Prefix Notation (10 points). Convert the infix notation used for binary operators in C++ code into prefix notation that indicates exactly what operators are being called and in what order. For example, this line of code: x = 2 + 2; would be converted into the following prefix notation with the usual C++ convention of using the "operator" keyword to name each operator: operator=(x, operator+(2, 2)) Assume that variables x and y have been declared as being of type int. Consider the following lines of code: y = 2 * 3; x = y + 4 * 5; cout << "x = " << x << endl; cout << "sum = " << (x + y) << endl; y *= 2 * y - x; Write the prefix version of this code below. 4. String Manipulation (15 points). Write a function called underline that takes an istream containing an input file as a parameter and that prints to cout the same text with certain lines underlined. The lines to be underlined all begin with a period. The period should not be printed. You should print the text that follows the period on a line by itself followed by a line of dashes equal in length to the text that follows the period. For example, suppose that a file called underline.txt stores this text: .Statement of Purpose I didn't expect to major in computer science until I took cse142. .High School Performance I graduated in the top 10% of my high school class. .College Performance I have done well in my college classes, with an overall gpa of 3.5. Then the following lines of code: ifstream input("underline.txt"); underline(input); should produce the following output: Statement of Purpose -------------------- I didn't expect to major in computer science until I took cse142. High School Performance ----------------------- I graduated in the top 10% of my high school class. College Performance ------------------- I have done well in my college classes, with an overall gpa of 3.5. Notice that input lines can be blank lines. You may not construct any extra data structures to solve this problem other than strings. Also keep in mind that strings don't check indexes to make sure that they are legal, so you have to be careful not to refer to an index that is out of bounds. 5. Vector Manipulation (15 points). Write a function called interleave that takes three vectors of integers as parameters and that adds elements to the third vector so that it contains the result of interleaving the elements of the first two vectors. Two vectors are interleaved by taking elements in an alternating fashion from the two vectors (first value of first vector, first value of second vector, second value of first vector, second value of second vector, etc). For example, if vector1 and vector2 contain the following: vector1: [1, 8, 3, 9] vector2: [2, 12, 6, 14] then the call: interleave(vector1, vector2, result); should lead result to store the following sequence of values: [1, 2, 8, 12, 3, 6, 9, 14] One vector might be longer than the other, in which case any extra values from the longer vector should simply be added to the result. For example, given the following variables: vector1: [1, 8, 3, 9] vector2: [82, 7, 4, 2, 1, 6, 5, 0, 18] The call above should append to result the following sequence: [1, 82, 8, 7, 3, 4, 9, 2, 1, 6, 5, 0, 18] Notice that the first four values of the two vectors have been interleaved and the excess values from the second vector (1, 6, 5, 0, 18) have been added at the end. In this case the second vector was longer, but it could be the case that the first vector is longer. Either vector could also be empty. The function should not construct any extra data structures and it should not alter either of the first two parameters. You may assume that the result vector (third parameter) is empty when the function is called. 6. Class Definition (20 points). Write a class called rectangle that stores the dimensions of a rectangle (width and height). It should include the following constructor and member functions: rectangle(w, h) constructs a rectangle object with given dimensions to_string() returns a string representation of the rectangle area() returns the area of the rectangle (w * h) scale(n) scales the width and height by n Dimensions will be expressed as real numbers. The to_string method should return a string with the width followed by "x" followed by the height. The scale member function should adjust both dimensions. For example, scaling by 2.0 will double the dimensions while scaling by 0.5 will cut them in half. For example, the following code: rectangle r1(4.2, 5.3); rectangle r2(10.4, 300); cout << "r1 = " << r1.to_string() << ", area = " << r1.area() << endl; cout << "r2 = " << r2.to_string() << ", area = " << r2.area() << endl; r1.scale(2.3); r2.scale(3); cout << "r1 scaled = " << r1.to_string() << endl; cout << "r2 scaled = " << r2.to_string() << endl; would produce this output: r1 = 4.2x5.3, area = 22.26 r2 = 10.4x300, area = 3120 r1 scaled = 9.66x12.19 r2 scaled = 31.2x900 Remember that you should distinguish between const member functions and non-const member functions. Write your solution to the rectangle class below. 7. Programming (10 points). You are going to override the * operator to allow a client to multiply a string by an int. Python gives you the ability to multiply a string constant by an int indicating that you want multiple occurrences of that string. For example: ("hello" * 3) evaluates to the string "hellohellohello" It isn't possible in C++ to recreate this exact behavior for string constants because quoted strings are C-style strings stored as a pointer. But we can introduce this behavior for string objects, as in: string s1 = "hello"; string s2 = s1 * 3; string s3 = 5 * s1; This should leave s2 storing three occurrences of "hello" and s3 storing 5 occurences of "hello". That would mean that these lines of code: cout << s2 << endl; cout << s3 << endl; should produce the following output: hellohellohello hellohellohellohellohello Overloading the * operator in this way will require two different functions that deal with the two cases of whether the string comes first or the int comes first. You may assume that the integer passed to the function is not negative.
Stuart Reges
Last modified: Fri Feb 3 10:24:46 PST 2023