# Discussion 10-13: PIC Lab day!

Today we're meeting in the PIC lab. Here is a list of problems. They're roughly organized into categories, and the problems within each category are roughly ordered by difficulty. You should try to do at least one from each category, but you can choose the level. Even the "easy" ones are good practice for quickly writing working code!

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.

# 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 (3 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;
}
```

# Arithmetic

### Problem 1

Write a program that converts fractions to decimals.

Sample input/output:

```What is the numerator? 27
What is the denominator? 11
```

Advanced: What does your program do if the user chooses denominator = 0? Answer: Probably nothing good. Think about what would be a more reasonable response to this input and make your program do that instead.

Advanced: Let the user type their fraction all at once, with a / to indicate division. For example:

```What is your fraction? 27 / 11
```

### Problem 2

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