Skip to content

Commit a154182

Browse files
committed
Added materail for Section06.
1 parent 5db9775 commit a154182

File tree

13 files changed

+202
-12
lines changed

13 files changed

+202
-12
lines changed

Section06-Variables_and_Constants/readme.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,9 @@ sizeof some_variable // () is optional for variables
196196
- Enumerated constants
197197
- `enum` keyword
198198
- Defined constants
199-
- `#define`
199+
- `#define`
200+
- **Don't use defined constants in Modern C++**
201+
- i.e.
202+
```c++
203+
#define pi 3.1415926
204+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Coding Exercise 1: Declaring and Initializing Variables
2+
3+
You must declare a totale of **THREE** variables, each of a different type, to represent the employee's **name**, **age**, and **hourly_wage**.
4+
5+
- `age` should be an `int`
6+
- `name` should be a `string`
7+
- `hourly_wage` should be a `double`
8+
9+
- You must initialize the **hourly_wage** to **23.50**. In order to set the values
10+
- For **name** and **age** you must use `cin` and the extraction operator `>>` to allow the employee to enter their **name** and **age** in that order separated by a single space.
11+
12+
13+
## Solution
14+
15+
```c++
16+
#include <iostream>
17+
#include <string>
18+
using namespace std;
19+
20+
void employee_profile() {
21+
22+
//----WRITE YOUR CODE BELOW THIS LINE----
23+
24+
double hourly_wage {23.50};
25+
26+
string name;
27+
cin >> name;
28+
29+
int age {0};
30+
cin >> age;
31+
32+
//----WRITE YOUR CODE ABOVE THIS LINE----
33+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
34+
35+
cout << name << " " << age << " " << hourly_wage;
36+
}
37+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
1. Data values that do not change while a program executes are __________ .
2+
- [ ] doubles
3+
- [ ] variables
4+
- [x] literals
5+
- [ ] integer
6+
7+
2. All variables must have a __________ before they are used in a program.
8+
- [ ] value
9+
- [ ] display statement
10+
- [ ] comment
11+
- [x] definition
12+
13+
3. Storage locations in memory are represented by __________ .
14+
- [x] variables
15+
- [ ] square brackets []
16+
- [ ] literals
17+
- [ ] constants
18+
19+
4. String literals are always encloed in __________ .
20+
- [ ] square brackets []
21+
- [ ] curly braces {}
22+
- [ ] single quotes ''
23+
- [x] double quotes ""
24+
25+
5. Character literals are always enclosed in __________ .
26+
- [ ] square brackets []
27+
- [ ] curly braces {}
28+
- [x] single quotes ''
29+
- [ ] double quotes ""
30+
31+
6. A variable that hold only true or false values is of type __________ .
32+
- [ ] int
33+
- [x] bool
34+
- [ ] logical
35+
- [ ] double
36+
37+
7. The following expression determines how many bytes of storage are required to store a double on your computer:
38+
- [ ] `bytes(double)`
39+
- [ ] `length(double)`
40+
- [ ] `double`
41+
- [x] `sizeof(double)`
42+
43+
8. What is the output from the following code:
44+
45+
```c++
46+
int x;
47+
cout << x << endl;
48+
```
49+
50+
- [ ] 0
51+
- [x] some garbage value
52+
- [ ] null
53+
- [ ] "No value"
54+
55+
9. Which variable definitions is not legal in C++?
56+
- [ ] `int x = 100;`
57+
- [ ] `int x;`
58+
- [ ] `int x { 100 };`
59+
- [x] `int x = 100`
60+
61+
10. Which line in the following code will generate a compiler error?
62+
63+
```c++
64+
#include <iostream>
65+
using namespace std;
66+
67+
int main ()
68+
{
69+
const int min_age = 18;
70+
const int max_age = 35;
71+
min_age = 21;
72+
cout << "Minimum age is" << min_age << endl;
73+
return 0;
74+
}
75+
76+
- [x] 8 `min_age = 21;`
77+
- [ ] 7 `const int max_age = 35;`
78+
- [ ] 6 `const int min_age = 18;`
79+
- [ ] 9 `cout << "Minimum age is" << min_age << endl;`
Binary file not shown.

Section06-Variables_and_Constants/section6_source_code/Challenge/main.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,32 @@ This estimate is valid for 30 days
3030
using namespace std;
3131

3232
int main() {
33-
33+
34+
cout << "Hello, welcome to Frank's Carpet Cleaning Service" << endl;
35+
36+
cout << "\nHow many small room would you like cleaned?\t";
37+
int number_of_small_room{0};
38+
cin >> number_of_small_room;
39+
cout << "\nHow many large room would you like cleaned?\t";
40+
int number_of_large_room{0};
41+
cin >> number_of_large_room;
42+
43+
cout << "\nEstimate for carpet cleaning service" << endl;
44+
cout << "Number of small rooms: " << number_of_small_room << endl;
45+
cout << "Number of large rooms: " << number_of_large_room << endl;
46+
const double price_per_small_room {25.0};
47+
cout << "Price per small room: $" << price_per_small_room << endl;
48+
const double price_per_large_room {35.0};
49+
cout << "Price per large room: $" << price_per_large_room << endl;
50+
float cost = (price_per_small_room * number_of_small_room) + (price_per_large_room * number_of_large_room);
51+
cout << "Cost : $" << cost << endl;
52+
const double sales_tax {0.06};
53+
float tax = cost * sales_tax;
54+
cout << "Tax: $" << tax << endl;
55+
cout << "===============================" << endl;
56+
cout << "Total estimate: $" << cost + tax << endl;
57+
const int estimate_expiry {30}; // days
58+
cout << "This estimate is valid for " << estimate_expiry << " days" << endl;
3459

3560
cout << endl;
3661
return 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.apple.xcode.dsym.main</string>
9+
<key>CFBundleInfoDictionaryVersion</key>
10+
<string>6.0</string>
11+
<key>CFBundlePackageType</key>
12+
<string>dSYM</string>
13+
<key>CFBundleSignature</key>
14+
<string>????</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>1.0</string>
17+
<key>CFBundleVersion</key>
18+
<string>1</string>
19+
</dict>
20+
</plist>

Section06-Variables_and_Constants/section6_source_code/Constants/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int main() {
4141
int number_of_rooms {0};
4242
cin >> number_of_rooms;
4343

44-
const double price_per_room {32.5};
44+
const double price_per_room {30.0};
4545
const double sales_tax {0.06};
4646
const int estimate_expiry {30}; // days
4747

Section06-Variables_and_Constants/section6_source_code/PrimitiveTypes/main.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ int main() {
2424
long people_in_florida {20610000};
2525
cout << "There are about " << people_in_florida << " people in Florida" << endl;
2626

27-
long long people_on_earth {7'600'000'000}; // make sure this is a long long and not just a long
27+
long long people_on_earth {7'600'000'000}; // make sure this is a long long and not just a long
28+
// if compile error, may try to get rid of tick marks.
29+
// C++ version 14 and on should allow tick marks and ommit displaying.
2830
cout << "There are about " << people_on_earth << " people on earth" << endl;
2931

3032
long long distance_to_alpha_centauri {9'461'000'000'000};
@@ -34,7 +36,7 @@ int main() {
3436
/***********************************************
3537
* Floating point types
3638
**********************************************/
37-
float car_payment { 401.23};
39+
float car_payment {401.23};
3840
cout << "My car payment is " << car_payment << endl;
3941

4042
double pi {3.14159};
@@ -48,13 +50,14 @@ int main() {
4850
**********************************************/
4951
bool game_over {false};
5052
cout << "The value of gameOver is " << game_over << endl;
53+
// Notice: Output would be "The value of gameOver is 0" and not "The value of gameOver is false"
5154

5255
/***********************************************
5356
* Overflow example
5457
**********************************************/
5558
short value1 {30000};
5659
short value2 {1000};
57-
short product {value1 * value2};
60+
short product {value1 * value2};
5861

5962
cout << "The product of " << value1 << " and " << value2 << " is " << product << endl;
6063

Binary file not shown.

Section06-Variables_and_Constants/section6_source_code/SizeofOperator/main.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#include <iostream>
55
#include <climits> // make sure you include climits for integer types
6-
// Similar information for floating point numbers
7-
// is contained in <cfloat>
6+
// Similar information for floating point numbers
7+
// is contained in <cfloat>
88

99
using namespace std;
1010

@@ -25,8 +25,8 @@ int main() {
2525
cout << "float: " << sizeof(float) << " bytes." << endl;
2626
cout << "double: " << sizeof(double) << " bytes." << endl;
2727
cout << "long double: " << sizeof(long double) << " bytes." << endl;
28-
//
29-
//
28+
29+
3030
// use values defined in <climits>
3131
cout << "========================" << endl;
3232

@@ -36,7 +36,7 @@ int main() {
3636
cout << "short: " << SHRT_MIN << endl;
3737
cout << "long: " << LONG_MIN << endl;
3838
cout << "long long: " << LLONG_MIN << endl;
39-
//
39+
4040
cout << "========================" << endl;
4141

4242
cout << "Maximum values:" << endl;
@@ -45,7 +45,7 @@ int main() {
4545
cout << "short: " << SHRT_MAX << endl;
4646
cout << "long: " << LONG_MAX << endl;
4747
cout << "long long: " << LLONG_MAX << endl;
48-
//
48+
4949
// sizeof can also be used with variable names
5050
cout << "========================" << endl;
5151

@@ -59,6 +59,7 @@ int main() {
5959
cout << "wage is " << sizeof(wage) << " bytes." << endl;
6060
// or
6161
cout << "wage is " << sizeof wage << " bytes." << endl;
62+
// Notice: () is not necessary for variables
6263

6364

6465
return 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.apple.xcode.dsym.main</string>
9+
<key>CFBundleInfoDictionaryVersion</key>
10+
<string>6.0</string>
11+
<key>CFBundlePackageType</key>
12+
<string>dSYM</string>
13+
<key>CFBundleSignature</key>
14+
<string>????</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>1.0</string>
17+
<key>CFBundleVersion</key>
18+
<string>1</string>
19+
</dict>
20+
</plist>

0 commit comments

Comments
 (0)