Skip to content

Commit 4843d24

Browse files
committed
Added all section 11 material.
1 parent 9b5c375 commit 4843d24

File tree

28 files changed

+1519
-75
lines changed

28 files changed

+1519
-75
lines changed

Section11_Functions/README.md

+810
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,367 @@
1+
# Coding Exercise 21:
2+
3+
Create a program that will be used as a POS (Point of Scale) system in a resturant
4+
5+
```c++
6+
#include <iostream>
7+
#include <cmath>
8+
using namespace std;
9+
10+
void c_math_functions() {
11+
12+
double bill_total {102.78};
13+
int number_of_guests {5};
14+
15+
//DO NOT MODIFY THE CODE ABOVE THIS LINE----
16+
//----WRITE YOUR CODE BELOW THIS LINE----
17+
18+
// Initialization
19+
double individual_bill {bill_total / number_of_guests};
20+
double individual_bill_1 = floor (individual_bill); // https://www.programiz.com/cpp-programming/library-function/cmath/floor
21+
double individual_bill_2 = round (individual_bill); // https://www.programiz.com/cpp-programming/library-function/cmath/round
22+
double individual_bill_3 = ceil (individual_bill * 100) / 100; // https://www.programiz.com/cpp-programming/library-function/cmath/ceil
23+
24+
25+
//----WRITE YOUR CODE ABOVE THIS LINE----
26+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
27+
cout << "The individual bill at location 1 will be $" << individual_bill_1 << "\n" << "The individual bill at location 2 will be $" << individual_bill_2 << "\n" << "The individual bill at location 3 will be $" << individual_bill_3;
28+
}
29+
```
30+
31+
# Coding Exercise 22: Functions and Prototypes - Converting Temperatures
32+
33+
Create a program that will be used to convert Fahrenheit temperatures to Celcius and Kelvin temperatures through the use of two functions.
34+
35+
```c++
36+
#include <iostream>
37+
#include <cmath>
38+
using namespace std;
39+
40+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
41+
//----WRITE YOUR FUNCTION PROTOTYPES BELOW THIS LINE----
42+
43+
// Function prototypes
44+
double fahrenheit_to_celsius(double temperature);
45+
double fahrenheit_to_kelvin(double temperature);
46+
47+
//----WRITE YOUR FUNCTION PROTOTYPES ABOVE THIS LINE----
48+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
49+
50+
void temperature_conversion(double fahrenheit_temperature) {
51+
52+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
53+
//----WRITE YOUR FUNCTION CALLS BELOW THIS LINE----
54+
55+
// Call functions and store to variable
56+
double celsius_temperature = fahrenheit_to_celsius(fahrenheit_temperature);
57+
double kelvin_temperature = fahrenheit_to_kelvin(fahrenheit_temperature);
58+
59+
//----WRITE YOUR FUNCTION CALLS ABOVE THIS LINE----
60+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
61+
62+
cout << "The fahrenheit temperature " << fahrenheit_temperature << " degrees is equivalent to " << celsius_temperature << " degrees celsius and " << kelvin_temperature << " degrees kelvin.";
63+
}
64+
65+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
66+
//----WRITE YOUR FUNCTION DEFINITIONS BELOW THIS LINE----
67+
68+
double fahrenheit_to_celsius(double temperature) {
69+
return round(5 * (temperature - 32) / 9);
70+
}
71+
72+
double fahrenheit_to_kelvin(double temperature) {
73+
return (fahrenheit_to_celsius(temperature) + 273);
74+
}
75+
```
76+
77+
# Coding Exercise 23: Using Default Argument Values - Grocery List
78+
79+
In this exercise you will create a program that will be used to automatically print a grocery list. Most weeks the grocery list is the same and so you may begin by declaring the function prototype `print_grocery_list` which has the default argument values:
80+
`mangos = 13`
81+
`apples = 3`
82+
`oranges = 7`
83+
The function `print_grocery_list` has no return statement and so the return type of the function prototype should be `void`.
84+
*IMPORTANT:* Before declaring the function prototype, read the entire exercise to determine the proper order of arguments in the function parameter list. Remember that default argument values that do not change should be placed at the tail end of the parameter list, and those which change most often should be placed at the beginning.
85+
86+
```c++
87+
#include <iostream>
88+
using namespace std;
89+
90+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
91+
//----WRITE YOUR FUNCTION PROTOTYPE BELOW THIS LINE----
92+
93+
void print_grocery_list(int apples = 3, int oranges = 7, int mangos = 13);
94+
95+
//----WRITE YOUR FUNCTION PROTOTYPE ABOVE THIS LINE----
96+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
97+
98+
void modify_grocery_list() {
99+
100+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
101+
//----WRITE YOUR FUNCTION CALLS BELOW THIS LINE----
102+
103+
print_grocery_list();
104+
print_grocery_list(5);
105+
print_grocery_list(7, 11);
106+
107+
//----WRITE YOUR FUNCTION CALLS ABOVE THIS LINE----
108+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
109+
}
110+
111+
void print_grocery_list(int apples, int oranges, int mangos) { //----WRITE THE FUNCTION PARAMETER LIST WITHIN THE PARENTHESES
112+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
113+
cout << apples << " apples" << "\n" << oranges << " oranges" << "\n" << mangos << " mangos" << "\n";
114+
}
115+
```
116+
117+
# Coding Exercise 24: Overloading Functions - Calculating Area
118+
119+
Create a program taht computes the area of two shapes, a `square` and a `rectangle`, by calling the overload function `find_area`
120+
121+
```c++
122+
#include <iostream>
123+
#include <cmath>
124+
using namespace std;
125+
126+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
127+
//----WRITE YOUR FUNCTION PROTOTYPES BELOW THIS LINE----
128+
129+
int find_area(int side_length);
130+
double find_area(double length, double width);
131+
132+
//----WRITE YOUR FUNCTION PROTOTYPES ABOVE THIS LINE----
133+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
134+
135+
void area_calc() {
136+
137+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
138+
//----WRITE YOUR FUNCTION CALLS BELOW THIS LINE----
139+
140+
int square_area = find_area(2);
141+
double rectangle_area = find_area(4.5, 2.3);
142+
143+
//----WRITE YOUR FUNCTION CALLS ABOVE THIS LINE----
144+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
145+
146+
cout << "The area of the square is " << square_area << "\n" << "The area of the rectangle is " << rectangle_area;
147+
}
148+
149+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
150+
//----WRITE YOUR FUNCTION DEFINITIONS BELOW THIS LINE----
151+
152+
int find_area(int length) {
153+
return pow(length, 2);
154+
}
155+
156+
double find_area(double length, double width) {
157+
return (length * width);
158+
}
159+
```
160+
161+
# Coding Exercise 25: Passing Arrays to Functions - Print a Gust List
162+
163+
Create a program that will be used to `print` the `guest_list` to an event and then `clear` the `guest_list` when the event is over
164+
165+
```c++
166+
#include <iostream>
167+
#include <string>
168+
#include <typeinfo>
169+
using namespace std;
170+
171+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
172+
//----WRITE THE FUNCTION PROTOTYPES BELOW THIS LINE----
173+
174+
string print_guest_list(const string[], size_t);//----WRITE THE FUNCTION PARAMETER LIST WITHIN THE PARENTHESES
175+
void clear_guest_list(string[], size_t);//----WRITE THE FUNCTION PARAMETER LIST WITHIN THE PARENTHESES
176+
177+
//----WRITE THE FUNCTION PROTOTYPES ABOVE THIS LINE----
178+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
179+
180+
void event_guest_list() {
181+
182+
string guest_list[] {"Larry", "Moe", "Curly"};
183+
size_t guest_list_size {3};
184+
185+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
186+
//----WRITE THE FUNCTION CALLS BELOW THIS LINE----
187+
188+
print_guest_list(guest_list, guest_list_size);
189+
clear_guest_list(guest_list, guest_list_size);
190+
print_guest_list(guest_list, guest_list_size);
191+
192+
//----WRITE THE FUNCTION CALLS ABOVE THIS LINE----
193+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
194+
}
195+
196+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
197+
//----WRITE THE FUNCTION DEFINITION BELOW THIS LINE----
198+
199+
string print_guest_list(const string guest_list[], size_t guest_list_size) {//----WRITE THE FUNCTION PARAMETER LIST WITHIN THE PARENTHESES
200+
201+
//----WRITE THE FUNCTION BODY BELOW THIS LINE----
202+
for (size_t i=0; i < guest_list_size; ++i) {
203+
cout << guest_list[i] << endl;
204+
}
205+
206+
//----WRITE THE FUNCTION BODY ABOVE THIS LINE----
207+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
208+
return typeid(guest_list).name();
209+
}
210+
211+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
212+
//----WRITE THE FUNCTION DEFINITION BELOW THIS LINE----
213+
214+
void clear_guest_list(string guest_list[], size_t guest_list_size) {//----WRITE THE FUNCTION PARAMETER LIST WITHIN THEPARENTHESES
215+
216+
//----WRITE THE FUNCTION BODY BELOW THIS LINE----
217+
218+
for (size_t i=0; i < guest_list_size; ++i) {
219+
guest_list[i] = " ";
220+
}
221+
222+
// return guest_list;
223+
224+
//----WRITE THE FUNCTION BODY ABOVE THIS LINE----
225+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
226+
}
227+
```
228+
229+
# Coding Exercise 26: Using Pass by Reference - Print a Guest List
230+
231+
Create a program that will be used to `print` the `guest_list` to an event and then `clear` the `guest_list` when the event is over
232+
233+
```c++
234+
#include <iostream>
235+
#include <string>
236+
#include <typeinfo>
237+
using namespace std;
238+
239+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
240+
//----WRITE THE FUNCTION PROTOTYPES BELOW THIS LINE----
241+
242+
string print_guest_list(const string &, const string &, const string &);//----WRITE THE FUNCTION PARAMETER LIST WITHIN THE PARENTHESES
243+
void clear_guest_list(string &, string &, string &);//----WRITE THE FUNCTION PARAMETER LIST WITHIN THE PARENTHESES
244+
245+
//----WRITE THE FUNCTION PROTOTYPES ABOVE THIS LINE----
246+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
247+
248+
void event_guest_list() {
249+
250+
string guest_1 {"Larry"};
251+
string guest_2 {"Moe"};
252+
string guest_3 {"Curly"};
253+
254+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
255+
//----WRITE THE FUNCTION CALLS BELOW THIS LINE----
256+
257+
print_guest_list(guest_1, guest_2, guest_3);
258+
clear_guest_list(guest_1, guest_2, guest_3);
259+
print_guest_list(guest_1, guest_2, guest_3);
260+
261+
//----WRITE THE FUNCTION CALLS ABOVE THIS LINE----
262+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
263+
}
264+
265+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
266+
//----WRITE THE FUNCTION DEFINITION BELOW THIS LINE----
267+
268+
string print_guest_list(const string &guest_1, const string &guest_2, const string &guest_3) {//----WRITE THE FUNCTION PARAMETER LIST WITHIN THE PARENTHESES
269+
270+
//----WRITE THE FUNCTION BODY BELOW THIS LINE----
271+
272+
cout << guest_1 << endl;
273+
cout << guest_2 << endl;
274+
cout << guest_3 << endl;
275+
276+
277+
//----WRITE THE FUNCTION BODY ABOVE THIS LINE----
278+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
279+
string test_1 = typeid(guest_1).name(), test_2 = typeid(guest_2).name(), test_3 = typeid(guest_3).name();
280+
return test_1 + test_2 + test_3;
281+
}
282+
283+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
284+
//----WRITE THE FUNCTION DEFINITION BELOW THIS LINE----
285+
286+
void clear_guest_list(string &guest_1, string &guest_2, string &guest_3) {//----WRITE THE FUNCTION PARAMETER LIST WITHIN THE PARENTHESES
287+
288+
//----WRITE THE FUNCTION BODY BELOW THIS LINE----
289+
290+
guest_1 = " ";
291+
guest_2 = " ";
292+
guest_3 = " ";
293+
294+
//----WRITE THE FUNCTION BODY ABOVE THIS LINE----
295+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
296+
}
297+
```
298+
299+
# Codign Exercise 27: Implemeting a Recursive Function - Save a Penny
300+
301+
Create a program that calculate the `total_amount` of money that will be accumulated if you start with a penny and double it everyday for `n` number of days.
302+
303+
i.e.
304+
```txt
305+
Day 1: $0.01
306+
Day 2: $0.02
307+
Day 3: $0.04
308+
...
309+
...
310+
...
311+
Day 16: $327.68
312+
Day 17: $655.36
313+
Day 18: $1310.72
314+
```
315+
316+
```c++
317+
#include <iostream>
318+
#include <iomanip>
319+
using namespace std;
320+
321+
int function_activation_count {0};
322+
323+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
324+
//----WRITE THE FUNCTION PROTOTYPE BELOW THIS LINE----
325+
326+
double a_penny_doubled_everyday(int num_day, double amount);//----WRITE THE FUNCTION PARAMETER LIST WITHIN THE PARENTHESES
327+
328+
//----WRITE THE FUNCTION PROTOTYPE ABOVE THIS LINE----
329+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
330+
331+
void amount_accumulated() {
332+
333+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
334+
//----WRITE THE FUNCTION CALL BELOW THIS LINE----
335+
336+
int num_day {25};
337+
double amount {0.01};
338+
339+
double total_amount = a_penny_doubled_everyday(num_day, amount);
340+
341+
342+
//----WRITE THE FUNCTION CALL ABOVE THIS LINE----
343+
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
344+
cout << "If I start with a penny and doubled it every day for 25 days, I will have $" << setprecision(10) << total_amount;
345+
}
346+
347+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
348+
//----WRITE THE FUNCTION DEFINITION BELOW THIS LINE----
349+
350+
double a_penny_doubled_everyday(int num_day, double amount) {//----WRITE THE FUNCTION PARAMETER LIST WITHIN THE PARENTHESES
351+
function_activation_count++;
352+
//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
353+
//----WRITE THE FUNCTION BODY BELOW THIS LINE----
354+
355+
if (num_day == 1)
356+
return amount;
357+
return a_penny_doubled_everyday(num_day--, amount*2);
358+
359+
360+
//----WRITE THE FUNCTION BODY ABOVE THIS LINE----
361+
//DO NOT MODIFY THE CODE BELOW THIS LINE----
362+
}
363+
364+
int test_function_activation_count() {
365+
return function_activation_count;
366+
}
367+
```

0 commit comments

Comments
 (0)