# Practice Problem from last time

## Problem 1

Suppose I have a string called name that stores a name in the format

`LASTNAME, FIRSTNAME`

Write some code that will rearrange the string into

`FIRSTNAME LASTNAME`

Your code should work for all possible starting strings (i.e. anything that is two words, separated by a comma and a space).

# A setprecision example

We already learned about setprecision in lecture, and it's mostly straightforward. We'll do one quick example here.

```#include <iostream>
#include <iomanip>  // setprecision is part of the iomanip library
using namespace std;

int main() {

double example_number = 132.481;
const int prec_num = 4;

// Without the fixed or scientific option, this will display
// prec_num digits total
cout << setprecision(prec_num);
cout << "With setprecision(" << prec_num <<
"4): " << example_number << "\n";

// With the fixed option, this will display all digits to the left
// of the decimal, and prec_num digits to the right
cout << fixed;
cout << "With fixed: " << example_number << "\n";

// With the scientific option, this will display a total of prec_num+1
// digits in scientific notation (one digit before the decimal, the rest after)
cout << scientific;
cout << "With scientific: " << example_number << "\n";

// Each option remains in effect until overwritten by another option
// Ex: The setprecision(prec_num) from the beginning affects all other lines.
//    we can change it by adding another setprecision command later.

return 0;
}
```

# A setw example

setw is part of the iomanip library (you #include<iomanip> to use it). It can be used to format your output. Specifically, if I write

`cout << setw(SOME_NUMBER)`

Then my next time I use cout, the output will take up SOME_NUMBER spaces. I tried to write one big example problem that will show you everything you need to know. Here it is!

```#include<iostream>
#include<iomanip>  // Need this to use setw
using namespace std;

int main() {

cout << setw(40) << left << "Column 1.";
cout << setw(30) << left << "Column 2.";
cout << '\n';  // Keep '\n' separate from your setw statements
// Fitting an endline into 40 characters doesn't really make sense
// If you put '\n' into a setw, the code will compile and run, but your output will look gross

cout << setw(40) << left << "This output will fill 40 chars";
cout << setw(30) << left << "This will fill 30 chars";
cout << '\n';

cout << setw(40) << left << "Thing in column 1";
cout << setw(30) << left << "Thing in column 2";
cout << '\n';

cout << setw(40) << left << "Thing in column 1";
cout << setw(30) << left << "Thing in column 2";
cout << '\n';

cout << setw(40) << left << "Thing in column 1";
cout << setw(30) << left << "Thing in column 2";
cout << '\n';

cout << setw(40) << left << "Thing in column 1";
cout << setw(30) << left << "Thing in column 2";
cout << '\n';

cout << setw(40) << right << "Still 40 chars, aligned on the right";
cout << setw(30) << right << "Same for 30 chars";
cout << '\n';

cout << setw(40) << left
<< "If you use setw(40) but put more than 40 chars, it will ignore your setw command. ";
cout << setw(30) << left << "See how these two statements don't fit in nice columns";
cout << '\n';

return 0;
}

```

# Practice coding problems

## Problem 1

Write a program that converts fractions to decimals. Specifically, your program should input a fraction (in other words, input a numerator and a denominator) and output the decimal expansion of that fraction.

Ex: If my numerator is 5 and my denominator is 9, the program should output 0.555556.

## Problem 2

Write a program that converts improper fractions to mixed fractions.

Recall: An improper fraction is one where the numerator is larger than the denominator. For example, 9/5 or 10/3. A mixed fraction really just an integer plus a fraction, such as 1+4/5 or 3+1/3.

So your program should input a fraction (similar to problem 1), and the output should be the same fraction, but written as a mixed fraction.

Ex: If the numerator is 32 and the denominator is 5, your program should output 6+2/5. If the numerator is 3 and the denominator is 8, your program should output 0+3/8

## Problem 3

Write a program that inputs a number of seconds from the user, and outputs the same amount of time written in units of hours, minutes, and seconds.

For example, if the user inputs 3663 then your program should output 1 hour, 1 minute, and 3 seconds. If the user inputs 139 then your program should output 0 hours, 2 minutes, and 19 seconds.

Side note: Your computer actually does this. There's something called "Coordinated Universal Time". It measures time by counting the number of seconds passed since Thursday, Jan 1, 1970. This is often the way your computer keeps track of time. So all the time values in your computer are stored purely in units of seconds. To make it readable to us, it takes the value in seconds and converts it to a standard date. That's pretty much what I'm asking you to do, except they also keep track of days, months, and years.