CSE390C Sample Midterm handout #3
Spring 2022
1. Parameter Mystery (15 points). Consider the following program:
#include
#include
using namespace std;
void mystery(string & a, int & b, int c, string d, int & e) {
a += "!";
b--;
c = c + 5;
d += "?";
e = e + 10;
cout << a << " " << b << " " << c << " " << d << " " << e << endl;
}
int main() {
string a = "elon";
int b = 4;
int c = 6;
string d = "musk";
int e = 8;
mystery(a, b, c, d, e);
mystery(a, b, b, a, 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
#include
using namespace std;
void mystery(int a, int * b, string & c, int & d, string e) {
a++;
*b = a + 2;
c[0] = '*';
d = d + 3;
e[1] = '*';
cout << a << " " << b << " " << c << " " << d << " " << e << endl;
}
int main() {
int a = 5;
int * b = &a;
string c = "joe";
int d = 10;
string e = "biden";
mystery(a, b, c, d, e);
mystery(*b, &d, e, a, c);
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. String Manipulation (15 points). Write a function called replace_all that
takes as parameters an istream, a target character, and a replacement
character and that replaces all occurrences of the target character with the
replacement character. All output should go to cout. For example, suppose
that a file called sample.txt has the following text:
This is a sample input file.
It has several periods.
on different lines...
in.the.file.
Then the following lines of code:
ifstream input("sample.txt");
replace_all(input, '.', '!');
should produce the following output:
This is a sample input file!
It has several periods!
on different lines!!!
in!the!file!
Notice that the blank line in the input file is recreated in the output.
4. Vector Manipulation (15 points). Write a function called remove_zeros that
takes a vector of integers as a parameter and that moves any zeros in the
vector to the end of the vector, otherwise preserving the order of the list.
For example, if a variable called "list" stores the following values:
[7, 2, 3, 0, 4, 6, 0, 0, 13, 0, 78, 0, 0, 19, 14]
then the call:
remove_zeros(list);
should rearrange the values in the vector so that it stores the following:
[7, 2, 3, 4, 6, 13, 78, 19, 14, 0, 0, 0, 0, 0, 0]
Notice that the six zeros have been moved to the end of the vector and the
other values are in the same order as in the original list.
You are not allowed to use an auxiliary data structure such as a temporary
vector to solve this problem.
5. Class Definition (25 points). Write a class called multi_string that stores
a string and a count that represents a string with count occurrences of the
string. It should include the following constructor and member functions:
multi_string(n, str) constructs an object with n occurrences of str
to_string() returns a string representation of the text
get_count() returns the number of occurrences of the string
set_count(n) sets the count to the given value
For example, the following code:
multi_string s1(8, "hello");
multi_string s2(40, "*");
cout << s1.to_string() << endl;
cout << s2.to_string() << endl;
cout << "s1 count = " << s1.get_count() << endl;
cout << "s2 count = " << s2.get_count() << endl;
s1.set_count(12);
s2.set_count(60);
cout << s1.to_string() << endl;
cout << s2.to_string() << endl;
would produce this output:
hellohellohellohellohellohellohellohello
****************************************
s1 count = 8
s2 count = 40
hellohellohellohellohellohellohellohellohellohellohellohello
************************************************************
Remember that you should distinguish between const member functions and
non-const member functions.
6. Programming (15 points). Override the insertion operator (<<) so that it
will print a vector as a bracketed, comma-separated list of the values
in the vector. For example, if we execute this code:
vector primes {2, 3, 5, 7, 11, 13, 17, 19, 23};
cout << primes << endl;
we would expect to see this output:
[2, 3, 5, 7, 11, 13, 17, 19, 23]