Practice Midterm2 Problems

# Practice Midterm Problems

Solutions to many of the problems are available here.

Important: When solving these problems remember that you will not have a computer with you at the midterm. If you can't solve a problem without using the computer, then you should study that material more.

For the code-writing exercises I recommend working in notepad. You can always check your code in visual studio once you're finished.

The organization here is kind of weird... Sorry! Hopefully these links help

```  Problems about vectors/arrays
```

# Section 1: Problems that specifically use vectors/arrays

## Warm-up Problems

### Warm-up Problem 1

Create a vector of size 3. Then use the square brackets [] to initialize the values to 2, 3, 4 (respectively). Afterwards, cout each value.

Next, do the same thing with an array.

### Warm-up Problem 2

Create a vector of size 0. Then use the .push_back() function to add the values 2, 3, 4 to your vector. Afterwards, cout each value.

Note: We can't do this with arrays. Array capacity is constant, which means that if you declare an array with 0 elements, it will always contain 0 elements (which would be a pretty silly array).

### Warm-up Problem 3

Redo problems 1 and 2, but this time use a loop. You should only have one line that has the square brackets [] (or in problem 2, the .push_back() function).

Next, do the same thing for problem 1 with an array.

## Practice Problems

### Problem 1: Complete the code

This program defines a const int n at the start of main. It should then add the numbers 0 through n to the vector myInts. Your program should still work if I change the value of n (i.e. don't write your code specifically for n=5.)

Example: If we use n=5, the vector myInts should contain 0, 1, 2, 3, 4, 5 (in that order).

```#include<iostream>
#include<vector>

using namespace std;

int main() {

const int n = 5;
vector<int> myInts;
return 0;
}```

NOTE: You can test your code by cout-ing each element of your vector and checking that it works. Also, Problem 2 asks you to write code that will cout each element of your vector....

Next, rewrite your code to use an array instead of a vector

### Problem 2: Complete the code again

Write the function printVec that completes the code below.

```#include<iostream>
#include<vector>

using namespace std;

// Think about why we use constant reference here
void printVec(const vector<int>& myInts);

int main() {

vector<int> myInts1;
vector<int> myInts2;

for (int i = 0; i<7; i++) {
// This code adds 7 numbers to each vector
// It doesn't really matter what the numbers are. I just wanted to
// create a couple vectors to so that we can test the printVec function
myInts1.push_back((i + 3) * 2 % 5);
myInts2.push_back(i*(3 - i) + 4);
}
printVec(myInts1);  // Should print "1, 3, 0, 2, 4, 1, 3"
cout << endl;
printVec(myInts2);  // Should print "4, 6, 6, 4, 0, -6, -14"
return 0;
}

/* Write the code for printVec here.
The function printVec should accept a vector<int> as input, and it should print to the console
each of the vector entries, separated by a comma.
Example: If my vector contains 0, 6, -4, 2  (in that order), then printVec should print
0, 6, -4, 2     to the console. */
```

Don't worry about using an array here. We will talk about passing arrays into functions later.

# Section 2: Finding/Fixing Errors

The following programs/code segments contain errors. Some are compiler errors (the code won't build/run), and some are logic errors (the code will build and run, but won't behave properly). Find both and fix as many errors as you can (of both types).

### Problem A: 4 Errors

```/* This program is meant to output the numbers 0 through 10, then output
the same numbers in backwards order (10 through 0). */
int main() {
int i=0;

for(unsigned i=0; i==10; i++) {
cout << i << '\n';
}
while(i < 0) {
cout << i << '\n';
i--;
}
return 0;
}
```

### Problem B: 3 Errors (although it depends on how you count...)

```include <cmath>
include <iostream>
using namespace std;

int main() {
double x = 4;
double y = 8;
cout << d(x, y) << '\n';
return 0;
}

int d(x, y) {
double d = abs(x-y);
return d;
}
```

### Problem C: 3 Errors

```/* This program is supposed to output the first 10 terms of the
Fibonacci sequence (1,1,2,3,5,8,...) */

#include <iostream>
using namespace std;

int increment(int x1, int x2) {
int temp = x1;
x1 = x2;
x2 += x1;
return x2;
}

int main() {
int x1 = 1;  // Smaller fibonacci number
int x2 = 1;  // Bigger  fibonacci number
for (unsigned i = 0; i < 10; i++) {
cout << x1 << '\n';
increment;
}
return 0;
}
```

Try to figure out what the following code does, without running it! Afterwards run it to confirm your answers.

### Problem A

```#include <iostream>
using namespace std;

int main() {

int result = 0;
const int stop = 35;

for(int i=0; i<stop; i++)
if(i%3==0)
result += i;

cout << result << endl;

}
```

### Problem B

```#include <iostream>
using namespace std;

int main() {

int j=4;
while(j > 0) {
int i = j;
while (i > 0) {
cout << "*";
i--;
}
cout << endl;
j--;
}

return 0;
}
```

### Problem C

```#include <iostream>
using namespace std;

int main() {

int log = 0;
int n = 64;
while(n > 1) {
n = n/2;
log++;
}

cout << log << "\n";
return 0;

}
```

### Problem D

```#include <iostream>
using namespace std;

int main() {

int n, m;
for (n = 1; n <= 3; n++)
for (m = 3; m >= 1; m--)
cout << n << " times " << m
<< " = " << n*m << endl;

}
```

### Problem E

```#include <iostream>
using namespace std;

char mystery(int firstParameter, int secondParameter);

int main()
{
cout << mystery(10,9) << "ow\n";
}

char mystery(int firstParameter, int secondParameter)
{
if (firstParameter >= secondParameter)
return 'W';
else
return 'H';
}
```

### Problem F

```#include <iostream>
using namespace std;

void friendly();
void shy(int audienceCount);

int main()
{
friendly();
shy(6);
cout << "One more time:\n";
shy(2);
friendly();
cout << "End of program.\n";
return 0;
}

void friendly()
{
cout << "Hello\n";
}

void shy(int audienceCount)
{
if (audienceCount < 5)
return;
cout << "Goodbye\n";
}
```

# Section 4: Writing Code

Write code to solve the following problems.

### Problem A

Write a program that inputs a string from the user (via getline), and replaces every vowel in the string with a !. For the purpose of this program, 'y' is never a vowel. Example input/output:

```Input:  Hello!
Output: H!ll!!

Input:  Australia
Output: !!str!l!!
```

Use a for loop to solve this problem.

### Problem B:

Write a function that inputs a vector of integers, and does different things depending on the size of the vector. If the size is an even number, multiply each entry of the vector by 3. If the size is an odd number, delete the last entry of the vector.

Here is a short main method that you can use to test if your function is working

```  int main() {
vector<int> vec1(4);
vector<int> vec2(5);
for(int i=0; i<vec1.size(); i++) {
vec1.at(i) = i*i;
}  // Initialize vec1 to store 0, 1, 4, 9
for(int i=0; i<vec2.size(); i++) {
vec2.at(i) = i-2;
}  // Initialize vec2 to store -2, -1, 0, 1, 2

FUNCTION_NAME_HERE(vec1);
FUNCTION_NAME_HERE(vec2);
printVec(vec1);   // This is the same printVec from the earlier problem
printVec(vec2);   // These lines should print the edited versions of vec1 and vec2.
}
```

Don't worry about using an array here. We will talk about passing arrays into functions later.

### Problem C

Write a program that inputs integers from the user (via cin) and stores the input in a vector. After each input you should ask the user if they want to input another number (use a while loop to keep asking for input until the user says no). At the end of the program print all the integers to the screen (you can use the printVec function from problem #2).

```// Example input/output