14. Week 7 Thursday: Midterm Practise
≪ 13. Week 7 Tuesday: Vectors! | Table of Contents | 14.1. Some Midterm Practise Solutions ≫In preparation for the upcoming midterm, let’s get some more practise with old and new concepts alike. The following problems will focus primarily on functions and vectors, though loops and conditionals will invariably show up as well.
As with the previous set of practise problems, these problems are split into three broad categories:
- Predict the output
You are to read through a program or chunk of code and figure out what it does, typically by identifying the output it will produce. - Spot the error
Like the first type of problem, you’re given a program or chunk of code, but there may be something wrong with it. You are to identify the error and fix it so that it works as intended. - Do it yourself
You will be given a task or scenario, and you are to design and implement a program that performs said task.
You should try to solve the problems in this order, more or less.
Predict the Output
In each of the following, predict the output of the snippet of code provided. For problems like this, I highly recommend giving a description of complicated chunks of code (e.g. loops or functions) in pseudocode before working through the program.
Problem 1.
1#include <iostream>
2#include <string>
3
4using namespace std;
5
6void foo(string& s, char c1, char c2) {
7 for(int i = 0; i < s.length(); i++) {
8 if(s.at(i) == c1) {
9 s.at(i) = c2;
10 } else if(s.at(i) == c2) {
11 s.at(i) = c1;
12 return;
13 }
14 }
15}
16
17int main() {
18 string str = "C++ is a programming language.";
19 foo(str, 'i', 'z');
20 cout << str << endl;
21
22 foo(str, 'n', 'p');
23 cout << str << endl;
24
25 foo(str, 'z', 't');
26 cout << str << endl;
27
28 return 0;
29}
Problem 2.
1#include <iostream>
2#include <vector>
3
4using namespace std;
5
6// prints out the contents of an int vector,
7// separated by spaces.
8void print_vector(const vector<int>& v) {
9 for(int i = 0; i < v.size(); i++) {
10 cout << v.at(i) << ' ';
11 }
12 cout << endl;
13}
14
15vector<int> f(vector<int> v, int j) {
16 vector<int> w;
17 for(int i = v.size() - 1; i > 0; i -= j) {
18 w.push_back(v.at(i));
19 }
20
21 return w;
22}
23
24int main() {
25 vector<int> v = {1, 4, 9, 16, 25, 36, 49};
26 print_vector(f(v, 1));
27 print_vector(f(v, 4));
28 print_vector(v);
29
30 return 0;
31}
Problem 3.
1#include <iostream>
2#include <string>
3#include <vector>
4
5using namespace std;
6
7void f(vector<string>& v, char t) {
8 for(int i = 0; i < v.size(); i++) {
9 if(i < v.at(i).length()) {
10 v.at(i).at(i) = t;
11 }
12 }
13}
14
15int main() {
16 vector<string> names = {
17 "Alice", "Bilbo", "Candace", "Ding", "Ed"
18 };
19
20 f(names, '#');
21 for(int i = 0; i < names.size(); i++) {
22 cout << names.at(i) << endl;
23 }
24
25 return 0;
26}
Spot The Error
In each of the following examples, identify all of the errors in the code provided:
- For syntax errors, explain why the code has improper syntax, then correct the code so that it compiles.
- For other errors, identify which circumstances cause the program’s output to be incorrect. If applicable, provide an example of a user input that would cause such behaviour.
Problem 4. Half-Censored
The following program intends to accept a line of text as user input, then replace every odd digit (i.e., 1 3 5 7 9
) with an asterisk *
. For instance, an input abc123
should produce abc*2*
.
1#include <iostream>
2#include <string>
3
4using namespace std;
5
6bool is_odd_digit(char c) {
7 // odd digit
8 if('0' < c <= '9' && (c - '0') % 2 == 1) {
9 return true;
10 }
11
12 // even digit
13 else if('0' < c <= '9' && (c - '0') % 2 == 0) {
14 return false;
15 }
16}
17
18int main() {
19 string input;
20 getline(cin, input);
21
22 // replace all of the odd digits with *!
23 for(int i = 0; i < input.length(); i++) {
24 if(is_odd_digit(input.at(i))) {
25 input.at(i) = '*';
26 }
27 }
28
29 return 0;
30}
Problem 5. Triangle
The following program allows the user to enter a single integer \(n\), then prints out a triangle made of asterisks spanning \(n\) lines.
SAMPLE RUN:
INPUT 3
OUTPUT *
OUTPUT ***
OUTPUT *****
1#include <iostream>
2#include <string>
3#include <vector>
4
5using namespace std;
6
7int main() {
8 // accept user input
9 int n;
10 cin >> n;
11
12 // set up n lines of a single *
13 vector<string> triangle;
14 for(int i = 0; i < n; i++) {
15 triangle.push_back("*");
16 }
17
18 // for each i = n-1, n-2, ..., 1, repeat:
19 // add a * before and after each of the bottom i lines.
20 // add a space before and after the remaining lines.
21 for(int i = n - 1; i > 0; i--) {
22 for(int j = 0; j < n; j++) {
23 string line = triangle.at(j);
24
25 if(j < i) { line = " " + line + " "; }
26 else { line = "*" + line + "*"; }
27 }
28 }
29
30 // print out the triangle
31 for(int i = 0; i < n; i++) {
32 cout << triangle.at(i) << endl;
33 }
34
35 return 0;
36}
Do It Yourself
Problem 6. Number Filter I
Write a function that accepts a vector of strings as a parameter, then returns the number of strings containing numbers. Should the vector of strings be passed by value or passed by reference? Should it be a const
?
Problem 7. Number Filter II
Write a function that accepts a reference to a vector of doubles and two additional doubles, min
and max
, as parameters. Then, modify the vector of doubles so that it only contains items between min
and max
.
Problem 8. Split Decisions
Write a function that accepts a reference to a vector of ints and an additional integer \(P\). Then, rearrange the vector so that every element smaller than \(P\) comes before all of the elements larger than \(P\).
Challenge Problem 9. Pig Latin
Write a program that accepts a single sentence from the user as input, then converts said text to pig latin. You may assume the user’s input will always be a single line ending with a period.
SAMPLE RUN:
INPUT I love mapo tofu, a Sichuanese staple.
OUTPUT Iyay ovelay apomay ofutay, ayay Ichuanesesay taplesay.
Note: The precise rules of translation are up to you; the point of this exercise is to think about handling capitalisation, punctuation, and perhaps numbers programmatically.