SETTINGS
Appearance
Language
About

Settings

Select a category to the left.

Appearance

Theme

Light or dark? Choose how the site looks to you by clicking an image below.

Light Dark

Language

Preferred Language

All content on utk.claranguyen.me is originally in UK English. However, if content exists in your preferred language, it will display as that instead. Feel free to choose that below. This will require a page refresh to take effect.

About

"utk.claranguyen.me" details

Domain Name: claranguyen.me
Site Version: 3.0.1
Last Updated: 2019/08/18
Getting Started
If you missed lab or failed to copy my code from the projector during lab session, the files I typed/utilised during lab sections 1 and 2 are available here: [LINK]
Synopsis
Congratulations for making it past the first lab. Now things will get a little tougher. This lab will let you calculate how many trips you can make around a stack of dollar bills of a certain length. So if I stacked up a few billion dollars, I can find out how many times I can walk back-and-forth the same distance they cover if stacked perfectly.
Constants
There are going to be times in your programs where you want to place (or "hard-code") a value in multiple times. Here is an example of that:
Code (C++)
int main() {
	int val = 4;
	int one = val / 4;

	cout << val << " / " << 4 << " is equal to 1." << endl;
}
This program would output:
Output
4 / 4 is equal to 1.
The program's goal is to show that dividing any number by itself makes it equal to 1. However, we have "4" written in 3 different places! So if we wanted to use another number, we would have to change that in 3 different spots each time. This is where constants come in. Observe:
Code (C++)
const int NUMBER = 4;

int main() {
	int val = NUMBER;
	int one = val / NUMBER;

	cout << val << " / " << NUMBER << " is equal to 1." << endl;
}
The output is exactly the same. However, if we wanted to change the number we are dividing by, we can easily just change the value at the top.
Example: Feet to Miles
Let's make a more useful program that converts feet to miles. The result may have decimals. That is where the double data type comes into play. It is a number, but it allows decimals. Let's see what this program would look like:
Code (C++)
#include <iostream>

using namespace std;

int main() {
	double feet, miles;
	cout << "Enter Feet: ";
	cin >> feet;
	miles = feet / 5280;
	cout << "Total Miles: " << miles << endl;
}
If we inserted "124" as the feet, the program's output would look like this:
Output
Enter Feet: 124
Total Miles: 0.0234848
What is iomanip?
iomanip is another include library that you can include at the top of your program along with iostream. It is (likely) shorthand for "Input/Output Manipulator" since it lets you manipulate how to write to cout. It allows us to format text a lot more easily. Observe:

Example: Aligning text to the right

Code (C++)
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
	int number1 = 2;
	int number2 = 23;
	int number3 = 435;

	cout << "Number 1: " << setw(4) << right << number1 << endl;
	cout << "Number 2: " << setw(4) << right << number2 << endl;
	cout << "Number 3: " << setw(4) << right << number3 << endl;
}
This program would print out 3 numbers, but it would make sure that the number field is set to a width of 4 characters, and would make sure it would print from the right. Its output would look like this:
Output
Number 1:    2
Number 2:   23
Number 3:  435
To clarify what I meant, if you set the width to 4 and print from the right, you are given the following room to work with:
Output
Number 1:
          ^^^^
Then afterwards, it will print from the right of that field of 4.
Output
Number 1:    2
          ^^^^

Example: Forcing 2 decimal places on a double

iomanip also allows us to set the number of decimal places printed out. That can be done with two operators: fixed and setprecision(). Here is an example:
Code (C++)
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
	double value = 0.123456;
	cout << fixed << setprecision(2) << value << endl;
}
I already spoiled what the result would be:
Output
0.12
Why do we need "fixed"? Isn't "setprecision()" good enough? Well... not exactly. If we do not print out in "fixed" notation, C++ will lean toward printing out in scientific notation. Plus then "setprecision" will go toward the number of numbers printed out as opposed to the number of decimal places printed out.
The Lab
To start things off, it is given that a dollar bill is 0.0043 inches thick. This lab requires that the thickness of a dollar bill be a constant of the data type "double" named "DOLLAR_BILL_THICKNESS_INCHES". You are also given mathematical functions which are in the lab writeup.

You will input distance that you want to travel (in miles) and then how much money you want to stack up. Then you will calculate how many inches the stack of dollar bills would be if stacked perfectly.
Code (C++)
(dollars * DOLLAR_BILL_THICKNESS_INCHES)
Afterwards, you need to convert those inches to feet and store them into a variable, followed by converting the feet into miles. Finally, you will calculate the number of trips you can make around the dollar bills. The following formulas are provided to you for doing this:
Code (C++)
DOLLAR_FEET = DOLLAR_INCHES / 12.0
DOLLAR_MILES = DOLLAR_FEET / 5280.0
TRIPS = DOLLAR_MILES / TRAVEL_DISTANCE
Finally, you will print out the results with cout. However, you have a new secret weapon known as iomanip. Refer to the lab writeup linked above on what he wants your formatting to look like for the lab.