# Discussion 5-9: Midterm review!

During section, I gave advice on how to study + likely problem types. I typed some of the advice as I talked; you can find it here. We also talked about some basic vector usage and solved a simple problem; you can find that code here.

The rest of this page is a list of problems. They're roughly organized into categories, and the problems within each category are roughly ordered by difficulty. Solutions to some of the problems are available here.

Important note: On the test you're going to have pencil and paper only! You should work on these problems without using your computer at first, just by writing down your answer on a white sheet of unlined paper. Once you're done, THEN type it into a computer and test for errors. If you don't practice coding without the computer helping, it's real tough to write error-free code on the test!

Clarification for all coding questions: When I give sample input/output, the blue text should be printed by your program, while the red text shows what the user typed in that example.

# Arithmetic

### Problem 1

Write a program that finds the x-intercept for a line. The user should input the slope m and the y-intercept b. Note: the x-intercept means the x coordinate where the line crosses the x-axis, i.e. the x0 value such that ( x0 , 0 ) is a point on the line.

Sample input/output:

```Please input your line using the formula y = m*x + b.
m = 13
b = -1.6
The x-intercept for your line is: 0.123077
```

Advanced: Can you think of any inputs that would make your program stop working? (Think about division by 0.) Figure out a more reasonable response to this situation and make your program do that instead.

# Strings and chars

### Problem 1

Ask the user to type their full name (first and last). Then output just their last name.

Sample input/output:

```Please type your full name (first and last): Dave Clyde
```

### Problem 2

Ask the user to type their full name (first and last). Then output their initials.

Sample input/output:

```Please type your full name (first and last): Dave Clyde
```

### Problem 3

Ask the user to type a string with an even number of characters. Then output a rearranged version where you swap the first half of their string with the second half. (The "halves" make sense because we're assuming the string has even length.)

Sample input/output #1:

```Please type a string with an even number of characters:
abcdefgh
The new string is:
efghabcd
```

Sample input/output #2:

```Please type a string with an even number of characters:
Hello my name is Dave!
The new string is:
me is Dave!Hello my na
```

Sample input/output #3:

```Please type a string with an even number of characters:
hahahaha
The new string is:
hahahaha
```

# If statements

### Problem 1

Ask the user to type any number, with decimals allowed. Then tell the user whether their number was an integer or not.

Sample input/output #1:

```Please type any number: -13
```

Sample input/output #2:

```Please type any number: 12.48
Your number is not an integer.
```

Sample input/output #3:

```Please type any number: 956.000
```

### Problem 2

Ask the user to type any number, with decimals allowed. Then print out exactly one of the following statements (whichever one is true):

• Your number is less than 0.
• Your number is at least 0 but at most 1.
• Your number is larger than 1 but less than 10.
• Your number is at least 10.

Sample input/output #1:

```Please type any number: 40
Your number is at least 10.
```

Sample input/output #2:

```Please type any number: 1.0
Your number is at least 0 but at most 1.
```

### Problem 3

Ask the user to type two numbers. Then print out which was larger, or tell the user that it was a tie.

Sample input/output #1:

```Type your first number: 10
```

Sample input/output #2:

```Type your first number: -350
It's a tie!
```

### Problem 4

Ask the user to type THREE numbers. Then print out the largest of the three numbers.

Sample input/output #1:

```Type your first number: 10
The biggest number was 58.2.
```

### Problem 5

Ask the user to type three numbers again. This time, print out the MIDDLE (i.e. median) of the three numbers.

Sample input/output #1:

```Type your first number: 10
The middle number was 10.
```

# Loops

### Problem 1

Ask the user for a positive integer n. Then output n factorial. (n factorial is 1*2*...*n. Google it if you're still unclear.)

Sample input/output #1:

```Please type a positive integer: 3
3 factorial is 6.
```

Sample input/output #2:

```Please type a positive integer: 8
8 factorial is 40320.
```

### Problem 2

Ask the user to type a string (use getline). Then output the same string, but with all periods changed to exclamation points.

Sample input/output:

```Type a string: It was a dark and stormy night...
It was a dark and stormy night!!!
```

### Problem 3

Ask the user to type a string (use getline). Then output the same string, but with all spaces removed.

Sample input/output:

```Type a string: It was the best of times, it was the worst of times
Itwasthebestoftimes,itwastheworstoftimes
```

# Functions

To practice with functions, you can reorganize your code for any of the above problems to use a function instead. For example, here are modified versions of the first two Loops problems above:

### Problem 1

Write a function that takes a nonnegative int input and returns the factorial.

### Problem 2

Make a function that takes a string by reference and modifies the string by turning all periods into exclamation points.

In this section, I give you a program and you tell me what it does. If the code won't compile, that would be your answer. Otherwise you should explain what will be printed out. There may be several possibilities depend on what the user types! Notice you can easily check your answers for these problems, just by copy-pasting the code into Visual Studio and running it yourself.

### Problem 1

```#include <iostream>

using namespace std;

int main() {
int x = 4;

cout << "x = " << x << endl;
if ( x < 10 ) {
x = x + 100;
cout << "Increasing x!" << endl;
}
if ( x > 20 ) {
x = x - 1000;
cout << "x is far too big. Decreasing." << endl;
}
cout << "x = " << x << endl;

x = 4;
cout << "x = " << x << endl;
if ( x < 10 ) {
x = x + 100;
cout << "Increasing x!" << endl;
}
else if ( x > 20 ) {
x = x - 1000;
cout << "x is far too big. Decreasing." << endl;
}
cout << "x = " << x << endl;

return 0;
}
```

### Problem 2

```#include <iostream>

using namespace std;

int main() {
int x = 4;
int y = 5;

x = x + y;
y = x - y;
x = x - y;

cout << "x = " << x << ", y = " << y << endl;

return 0;
}
```

### Problem 3

```#include <iostream>
#include <string>

using namespace std;

int main() {
const string p = "swordfish";
string i;

cout << "Tell me something... " << endl;
getline( cin, i );

int x = i.find( p );
if ( x == string::npos ) {
}
else if ( p == i ) {
cout << "ACCESS GRANTED." << endl;
}
else {
i.erase(i.find(p), p.length());
cout << "\"" + i + "\" is a better way to say that." << endl;
}

return 0;
}
```

### Problem 4

```#include <iostream>

using namespace std;

int main() {

int m = 6;
int k = 2;
while ( m > 0 ) {
k *= m;
m /= 2;
}

cout << k << m << '\n';
return 0;
}
```

# Debugging

In this section, I give you the program and you find the errors. You can paste the code into Visual Studio and run some tests to help find the mistakes. It's also good practice to see if you can spot the errors just by looking at the code, without Visual Studio's help!

Note: Some of the errors below are invalid C++ code, i.e. the program won't compile. Others will run just fine but do the wrong thing. Both are equally important to be able to track down!

### Problem 1 (1 error)

```#include <iostream>

using namespace std;

int mian()
{
cout << "Hello World!" << endl;
return 0;
}
```

### Problem 2 (2 errors)

```#include <iostream>

using namespace std;

int main() {
double height = 0;
double mass = 0;

// Note: I promise these values are right. This isn't a physics class.
double gravity = 6.674e-11; // 6.674*10^(-11) N(m/kg)^2
double earthRadius = 6.371e6; // 6371 km, expressed in meters
double earth_mass = 5.9736e24; // measured in kg

// E = G*m*M / R^2
double gravitationalPotential = gravity * earth_mass * mass / (earthRadius + height) * (earthRadius + height);

cout << "What is the mass of the object?" << endl;
cin >> mass;
cout << "How far above the ground is it?" << endl;
cin >> height;

cout << "The object has " << gravitationalPotential << " N of energy." << endl;

return 0;
}
```

### Problem 3 (5 errors)

```#include <iostream>

using namespace std;

int main() {
int month;
cout << "What month is it? (1-12)" << endl;
cin >> month;
cout << "The current season is ";
if ( 1 <= month <= 3 ) {
cout << "winter";
} else if ( 4 <= month <= 6 ) {
cout << "spring";
} else if ( 7 <= month <= 9 ) {
cout << "summer";
} else if ( 10 <= month <= 12 ) {
cout << autumn;
} else {
cout << "unknown because you didn't enter a real month";
}
cout << "!" << endl;

return 0;
}
```

### Problem 4 (4 errors)

```#include <iostream>
#include <string>

int main() {
string response;

cout << "Ennyn Durin Aran Moria. Pedo mellon a Minno. Im Narvi hain echant. Celebrimbor o Eregion tethant. I thiw hin." << endl;
cout << "The Doors of Durin, Lord of Moria. Speak friend and enter. I Narvi made them. Celebrimbor of Hollin drew these signs." << endl;

cin >> response

if ( response == "mellon" ) {
cout << "The star shines out briefly and fades again. Silently, a great "
<< "doorway is outlined, though not a crack or joint was visible "
<< "before. Slowly it divides in the middle and swings outward "
<< "inch by inch, until both doors lie back against the wall. "
<< "Through the opening a shadowy stair can be seen climbing steeply "
<< "up; but beyond the lower steps the darkness is deeper than the night."
<< endl;
cout << endl << "Moria lies before you. << endl;
}
else if ( response == "open sesame" ) {
cout << "The side of the mountain shakes with mighty force, and a cave "
<< "opens itself before you. Within lie the riches of the forty "
<< "thieves, treasures beyond your wildest dreams." << endl;
}
else {
cout << "Nothing happens..." << endl;
}

return 0;
}
```

### Problem 5 (1 error)

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

int main() {
int x;
int y;
cout << "What is x?" << endl;
cin >> x;
cout << "What is y?" << endl;
cin >> y;

// Switch x and y if necessary so that x >= y.
if ( x < y )
cout << "x should not be less than y.";
int temp = x;
x = y;
y = temp;
cout << " Swapped x and y." << endl;

cout << "OK, now x is " << x << " and y is " << y << endl;
cout << y <<  " <= " << x << endl;
return 0;
}
```

### Problem 6 (1 error)

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

int main() {
cout << "Guess my secret number! It's between 1 and 100\n";
int guess;
cin >> guess;
if (guess = 16)
cout << "Wow! You got it right!\n";
else
cout << "Nope. Better luck next time.\n";
return 0;
}
```