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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
Lab4 Code and Runs txt Benjamin Schroeder
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
6.8 Exercises
pp 132-133
5 pts #1-9
Submit: value of “a” after the expression is executed
1) a= sqrt(9.0); ... a = 3.0
2) a= sqrt(-9.0); ... -nan(ind)
3) a = pow(2.0,5); ... a= 32
4) a = pow(2.0,-2); ... a= .25
5) a = ceil(5.1); ... a= 6
6) a = ceil(5.9); ... a =6
7) a = floor(5.1); ... a = 5
8) a = floor(5.9); ... a = 5
9) a = sqrt(pow(abs(-2),4)); a=4
//int main() // example code
//{
// float a = 0;
// a = sqrt(pow(abs(-2),4));
// cout << a;
//}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
9.3_1 Exercises
p 207
10 pts #1
Submit: code & run
///// CODE /////
#include <iostream>
using namespace std;
float average(float, float, float);
int main()
{
average(3.2, 6.0, 8.6);
return 0;
}
float average(float x, float y, float z)
{
cout << "The average of these three #'s is : " << (x + y + z) / 3 << ".\n";
return 0.0;
}
//// RUN ////
Run from 9.3_1 p207
The average of these three #'s is : 5.93333.
C:\Users\Lenovo\source\repos\Lab4_Schroeder\Debug\Lab4_Schroeder.exe (process 6992) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
9.4_1 Learn by Doing Exercises
//p 214
//10 pts #1
//// CODE ////
#include <iostream>
using namespace std;
void GetInput(float&, int&);
void CalcRaise(float&, int);
int CalcBonus(int); //Pass by value
void PrintCalculations(int, float, int);
int main()
{
int years_service = 0;
float salary = 0.0;
while (salary <= 0)
{
GetInput(salary, years_service);
cout << "Salary is : " << salary << ".\n";
}
CalcRaise(salary,years_service);
int bonus = CalcBonus(years_service);
PrintCalculations(years_service, salary, bonus);
return 0;
}
void GetInput(float& sal, int& years_serv) // salary, and years_service have been passed by reference (Global Changes)
{
cout << "Enter the employee's salary: ";
cin >> sal;
if (sal > 0)
{
cout << "Enter the employee's years of service: ";
cin >> years_serv;
}
}
void CalcRaise(float& salary, int years_service) //copy of years_service has been sent here
{
if (years_service > 10)
{
salary = salary*1.1;
}
else if (years_service >= 5 && years_service <= 10)
{
salary = salary*1.05;
}
else
{
salary = salary*1.02;
}
}
int CalcBonus(int years_service) //copy of years_service has been sent here
{
int bonus = 500 * (years_service / 2);
return bonus;
}
void PrintCalculations(int years_service, float salary, int bonus) ////copy of years_service, salary, and bonus has been sent here
{
cout << "\n*******************************************************************************************\n\n";
cout << "\t\tThis employee's new salary is: $" << salary<<".\n";
cout << "\t\tAfter " << years_service << " years of service, they have earned a bonus of: $" << bonus << ".\n\n";
cout << "*******************************************************************************************";
}
//// RUN ////
Run from 9.4_1 p214
Enter the employee's salary: 25000
Enter the employee's years of service: 12
Salary is : 25000.
*******************************************************************************************
This employee's new salary is: $27500.
After 12 years of service, they have earned a bonus of: $3000.
*******************************************************************************************
C:\Users\Lenovo\source\repos\Lab4_Schroeder\Debug\Lab4_Schroeder.exe (process 13704) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
9.5_1 Learn by Doing Exercises p 216
# 1 Write a program to display the time in 3 different formats
//// CODE ////
# include<iomanip>
void getTime(int& hour, int& min, int& sec);
void displayTime(int,int,int,int F=3);
int main()
{
int hour = 0, min=0, sec=0;
int F;
getTime(hour, min, sec);
cout << "\n\n";
displayTime(hour, min, sec);
displayTime(hour, min, sec, 1);
displayTime(hour, min, sec, 2);
return 0;
}
void getTime(int& hour, int& min, int& sec) // function to ask for the time
{
hour = -1;
min = -1;
sec = -1;
cout << "Please enter the Time:\n";
while (hour > 24 || hour < 0)
{
cout << "\tHour (0 to 24): ";
cin >> hour;
}
while (min > 59 || min < 0)
{
cout << "\tMinute (0 to 59):";
cin >> min;
}
while (sec > 59 || sec < 0)
{
cout << "\tSecond(0 to 59): ";
cin >> sec;
}
}
void displayTime(int hour,int min,int sec,int F) //function to display the time , format depending upon F
{
string ampm = "am";
if (F == 1)
cout << "\t24 hr notation: " << setw(2) << setfill('0') << hour << ":" << setw(2) << setfill('0') << min << ":" << setw(2) << setfill('0') << sec << "\n";
else if (F == 2)
cout << "\tMilitary time: " << setw(2) << setfill('0') << hour << setw(2) << setfill('0') << min << ":" << setw(2) << setfill('0') << sec << "\n";
else if (F == 3)
{
if (hour > 12)
{
hour = hour - 12;
ampm = "pm";
}
cout << "\tStandard format: " << hour << ":" << setw(2) << setfill('0') << min << ":" << setw(2) << setfill('0') << sec << ampm<<"\n";
}
}
Run from 9.5_1 //// RUN ////
Please enter the Time:
Hour (0 to 24): 16
Minute (0 to 59):1
Second(0 to 59): 8
Standard format: 4:01:08pm
24 hr notation: 16:01:08
Military time: 1601:08
C:\Users\Lenovo\Source\Repos\cst116-lab4-BensProgramma\x64\Debug\CST116F2021-Lab4.exe (process 9696) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//9.13_1 Debugging Exercises
//pp 226 - 229
//10 pts
//// CORRECTED CODE ////
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
const int DAYS_PER_YEAR = 365;
int GetAge();
int CalcDays(int);
void PrintResults(int, int);
int main()
{
int age = 0;
int days = 0;
// Breakpoint 1
// Put breakpoint on the following line
age = GetAge();
days = CalcDays(age);
// Breakpoint 2
// Put breakpoint on the following line
PrintResults(days, age);
return 0;
}
// FUNCTIONS
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 days, int age)
{
cout << age << "! Boy are you old!\n";
cout << "Did you know that you are at least " << days << " days old?\n\n";
}
///// RUN /////
Run from 9.13_1 Debugging Exercise
Please enter your age: 33
33! Boy are you old!
Did you know that you are at least 12045 days old?
C:\Users\Lenovo\source\repos\Chapter9_Debug\Debug\Chapter9_Debug.exe (process 21340) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
9.14_1 Programming exercises
p229
// Draw A rectangle
//// CODE ////
#include <iostream>
#include<string>
using namespace std;
void DrawRect(int,int);
int main() // Program to draw a rectangle on the screen of user defined dimensions
{
int height = 0;
int width = 0;
cout << "Enter the dimensions of the rectangle I'm going to print on the screen\n";
cout << "Height: ";
cin >> height;
cout << "Width: ";
cin >> width;
DrawRect(height, width);
return 0;
}
void DrawRect(int height,int width) //Function that draws the rectangle
{
//Top
string top(width,char(196));
top[0] = char(218);
top[width-1] = char(191);
cout << top<<"\n";
//Sides
string sides(width, char(32));
for (int i=1; i<height-2;i++)
{
sides[0] = char(179);
sides[width-1] =char(179);
cout << sides<<"\n";
}
//Bottom
string bottom(width, char(196));
bottom[0] = char(192);
bottom[width-1] = char(217);
cout << bottom;
}
///// RUN //////
Run from 9.14_1
Enter the dimensions of the rectangle I'm going to print on the screen
Height: 10
Width: 25
┌───────────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└───────────────────────┘
C:\Users\Lenovo\Source\Repos\cst116-lab4-BensProgramma\x64\Debug\CST116F2021-Lab4.exe (process 23708) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|