1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
//Code by Jordan Harris-Toovy for CST116-01P Lab 4, October 2021
#include <iostream>
#include <iomanip>
using namespace std;
//7a - 9.3LBD#1
/*
float GetAvg(void);
int main(void)
{
float userAverage = 0.0F;
userAverage = GetAvg();
cout << "The average of the three values is: " << userAverage << endl;
return (0);
}
float GetAvg(void)
{
float userVal1, userVal2, userVal3, averageVal = 0.0F;
cout << "Enter value 1: ";
cin >> userVal1;
cout << endl;
cout << "Enter value 2: ";
cin >> userVal2;
cout << endl;
cout << "Enter value 3: ";
cin >> userVal3;
cout << endl;
averageVal = (userVal1 + userVal2 + userVal3) / 3.0F;
return averageVal;
}
*/
//7b - 9.4LBD#1
/*
void GetInput(float&, int&);
void CalcRaise(float&, int&);
int CalcBonus(int);
void PrintCalculations(int, float, int);
int main()
{
float sal = 0.0F;
int year_served = 0, bonus = 0;
GetInput(sal, year_served);
CalcRaise(sal, year_served);
bonus = CalcBonus(year_served);
PrintCalculations(year_served, sal, bonus);
return (0);
}
void GetInput(float& salary, int& years_service)
{
std::cout << "Enter the employee's salary: ";
std::cin >> salary;
std::cout << "\nEnter the employee's years of service: ";
std::cin >> years_service;
}
void CalcRaise(float& salary, int& years_service)
{
if (years_service > 10)
{
salary *= 1.1;
}
else if (years_service > 5)
{
salary *= 1.05;
}
else
{
salary *= 1.02;
}
}
int CalcBonus(int years_service)
{
int bonus = 0;
bonus = (years_service / 2) * 500;
bonus += (years_service % 2) * 500;
return bonus;
}
void PrintCalculations(int years_service, float salary, int bonus)
{
cout << endl << "For the employee's " << years_service << " years of service: " << endl;
cout.setf(ios::fixed);
cout << setprecision(2) << "Their salary will be changed to " << salary << " USD per hour" << endl
<< "And they will receive a bonus of " << bonus << " USD" << endl;
}
*/
//7c - 9.5LBD#1
/*
void getTimeFromUser(int& hours, int& minutes, int& seconds);
void displayTime(int& hours, int& minutes, int& seconds, int MODE = 1);
int main(void)
{
int h, m, s, MODE = 0;
getTimeFromUser(h, m, s);
while ((MODE != 1) && (MODE != 2) && (MODE != 3))
{
cout << "Please chose the time format to display:" << endl << "1) - 24 Hour time" << endl << "2) - Military time" << endl << "3) - 12 Hour time (standard)" << endl;
cin >> MODE;
if ((MODE != 1) && (MODE != 2) && (MODE != 3))
{
cout << "Invalid mode" << endl;
}
}
cout << endl << "The time is ";
displayTime(h, m, s, MODE);
return (0);
}
void getTimeFromUser(int& hours, int& minutes, int& seconds)
{
int check = 0;
while (check == 0)
{
cout << "Please enter the current time:" << endl << "Hours: ";
cin >> hours;
cout << "Minutes: ";
cin >> minutes;
cout << "Seconds: ";
cin >> seconds;
cout << endl;
if ((hours >= 24) || (minutes >= 60) || (seconds >= 60))
{
check = 0;
cout << "Invalid time" << endl;
}
else
{
check = 1;
}
}
}
void displayTime(int& hours, int& minutes, int& seconds, int MODE)
{
switch (MODE)
{
case 1:
cout << setfill('0') << setw(2) << hours << ":" << setfill('0') << setw(2) << minutes << ":" << setfill('0') << setw(2) << seconds << endl;
break;
case 2:
cout << setfill('0') << setw(2) << hours << setfill('0') << setw(2) << minutes << endl;
//It is unclear if military time format uses seconds, but most sources claim that it does not.
break;
case 3:
if (hours >= 12)
{
hours %= 12;
cout << hours << ":" << setfill('0') << setw(2) << minutes << ":" << setfill('0') << setw(2) << seconds << " PM" << endl;
}
else
{
cout << hours << ":" << setfill('0') << setw(2) << minutes << ":" << setfill('0') << setw(2) << seconds << " AM" << endl;
}
break;
default:
cout << endl << "Error" << endl;
break;
}
}
*/
//8b - 9.14PE#1
/*
void DisplayRect(int width, int height);
int main(void)
{
int userW = 0, userH = 0;
//Get user input
while ((userW <= 1) || (userH <= 1))
{
cout << "Enter width and height of the rectangle: " << endl;
cin >> userW >> userH;
cout << endl;
if ((userW <= 1) || (userH <= 1))
{
cout << "Size too small" << endl;
}
}
DisplayRect(userW, userH);
return (0);
}
void DisplayRect(int width, int height)
{
for (int idxY = 0; idxY <= height; idxY++)
{
if (idxY == 0) //Top segment
{
cout << (char)218;
for (int fillW = 0; fillW <= width - 3; fillW++)
{
cout << (char)196;
}
cout << (char)191 << endl;
}
else if (idxY < height) //Middle segments
{
cout << (char)179 << setw(width - 1) << (char)179 << endl;
}
else //Bottom segment
{
cout << (char)192;
for (int fillW = 0; fillW <= width - 3; fillW++)
{
cout << (char)196;
}
cout << (char)217 << endl;
}
}
}
*/
//8a - 9.13-Debug-Ex
//Note: code from textbook, modified my Jordan Harris-Toovy. Answers to asked questions in the Textual Work file
/*
* File: Chapter 9 Debug.cpp
*
* General Instructions: Complete each step before proceeding to the
* next.
*
* Debugging Exercise 1
*
* 1) Insert a breakpoint on the lines indicated in the code.
* 2) Run to Breakpoint 1.
* 3) Place a watch on age and days.
* 4) Add another watch using &age for the name. This will display
* the address of age.
* 5) Write down the address of age.
* 6) Step Into the code for the function GetAge.
* 7) The execution continues to the function header for GetAge.
* 8) Step into one more time.
* 9) Why did the address of age and value change?
* 10) Step over the cout and cin statements.
* 11) Verify the value entered is stored properly in age.
* 12) Step into until the flow returns to main.
* 13) Step over one more time.
* 14) Why didn't the value entered get transferred back to main?
* 15) Stop debugging and fix the error.
* 16) Run to Breakpoint 1.
* 17) Step over the function call to GetAge.
* 18) Verify that the value entered was returned and stored
* correctly from GetAge.
* 19) Stop debugging.
*
* Debugging Exercise 2
*
* 1) Run to Breakpoint 1.
* 2) Step over the call to GetAge.
* 3) Step into CalcDays.
* 4) Step into one more time so that the current line is the
* calculation.
* 5) Why is age greyed out in your watch window?
* 6) Stop debugging.
*
* Debugging Exercise 3
*
* 1) Run to Breakpoint 2.
* 2) When asked, enter the value of 20 for your age.
* 3) Verify that the variable age is 20 and the variable days
* is 7300.
* 4) Step into the PrintResults function.
* 5) Age is 7300? Not even Ralph is that old.
* 6) Why did the values for both variables change?
* 7) Stop debugging and fix the error.
*
* Debugging Exercise 4
*
* 1) Run to Breakpoint 2.
* 2) Display your Call Stack window.
* 3) View the contents of the window and notice that the top
* function on the stack is main.
* 4) Step into the PrintResults function.
* 5) Notice that the call stack now shows PrintResults on top of
* the stack.
*/
/*
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
const int DAYS_PER_YEAR = 365;
int GetAge();
int CalcDays(int age);
void PrintResults(int age, int days);
int main()
{
int age = 0;
int days = 0;
// Breakpoint 1
// Put breakpoint on the following line
age = GetAge(); //Fixed issue where function was not passing back to a variable
days = CalcDays(age);
// Breakpoint 2
// Put breakpoint on the following line
PrintResults(age, days);
return 0;
}
int GetAge()
{
int age;
cout << "Please enter your age: ";
cin >> age;
return age;
}
int CalcDays(int years)
{
int days;
days = years * DAYS_PER_YEAR;
return days;
}
void PrintResults(int age, int days) //Fixed an issue where input names were swapped
{
cout << age << "! Boy are you old!\n";
cout << "Did you know that you are at least " << days << " days old?\n\n";
}
*/
|