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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
//Tyler Taormina
//Lab 8 Text file
// CST 116
CST116
Module 8: Lab 8
================================================================================================
13a
11.7 Exercises
p 317
4 pts #5-6
Submit: Statements for 5, Judgements for 6
5.
a.) .is_open()
b.)
int num_data = 0;
//open file
ifstream input ("lab8.txt");
// check open file
if (input.is_open())
{
// priming
input >> lname[num_data] >> id[num_data] >> age[num_data];
}
else
cout << "File not opened" << endl;
return 0;
c.)
for the above ^^
input.close();
6.
a. false
b. true
c. false
d. true
e. true
================================================================================================
13b
11.9 Learn by Doing Exercises
p 323
10 pts #1: write a full program to call the function
Submit: code & run
CODE:
// Tyler Taormina
// CST 116
// Nov 2021
// Lab 8 Exercises
#include <iostream>
using namespace std;
void findMedian(float arr[], int);
float GetValues (float values[]);
#define MAX 20
int main ()
{
int num_values;
float values [MAX] = {0};
num_values = GetValues(values);
cout << "Here is our list..." << endl;
for (int i = 0; i < num_values; i++)
cout << values[i] << " ";
cout << endl;
cout << "===================================================" << endl;
findMedian(values, num_values);
return 0;
}
float GetValues (float values[])
{
int num_values = 0;
char cont = 'n';
do
{
cout << "Enter a number: ";
cin >> values[num_values++];
cout << "Enter another values (y/n)? ";
cin >> cont;
} while (toupper (cont) == 'Y' &&
num_values < MAX);
return num_values;
}
void findMedian(float arr[], int num_val)
{
float x;
if ((num_val % 2) == 0)
{
x = (arr[num_val/2] + arr[(num_val/2) - 1])/2;
cout << "Even number Array. We will average the two middle most numbers to determine the median." << endl;
cout << "The median is: " << x << endl;
}
else
{
cout << "Odd number Array. We will find the number in the middle of the list which determines the median." << endl;
cout << "The median is: " << arr[num_val/2] << endl;
}
}
RUN:
Enter a number: 1
Enter another values (y/n)? y
Enter a number: 2
Enter another values (y/n)? y
Enter a number: 3
Enter another values (y/n)? y
Enter a number: 4
Enter another values (y/n)? n
Here is our list...
1 2 3 4
===================================================
Even number Array. We will average the two middle most numbers to determine the median.
The median is: 2.5
till-t@Air:~/code/cst115-lab8-till-t|master⚡ ⇒ ./a.out
Enter a number: 1
Enter another values (y/n)? y
Enter a number: 2
Enter another values (y/n)? y
Enter a number: 3
Enter another values (y/n)? n
Here is our list...
1 2 3
===================================================
Odd number Array. We will find the number in the middle of the list which determines the median.
The median is: 2
================================================================================================
13c
11.13 Debugging Exercises
pp 333-336
10 pts
Submit: code & runs
CODE:
#include <iostream>
#include <fstream> // For the files!!!!
#include <iomanip> // For manipulators & formatting options
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::ios;
using std::resetiosflags;
using std::setiosflags;
using std::ifstream;
using std::ofstream;
const int EMPLOYEES = 20;
const int MAX = 21;
int ReadData ( ifstream &inFile, ofstream &outFile, char name[][MAX], int age[] );
void WriteOutputFile ( ofstream &outFile, char name[][MAX], int age[],
int counter );
void PrintTotalsAndSummary ( ofstream &out, int totalRecords );
int main ( )
{
char name[EMPLOYEES][MAX];
int age[EMPLOYEES];
int record_counter ( 0 );
ifstream inFile;
// Notice how this automatically opens the file
ofstream outFile ( "debug_report.txt" );
inFile.open ( "debug.txt");
if ( inFile.is_open ( ) )
{
record_counter = ReadData ( inFile, outFile, name, age );
inFile.close ( );
if ( outFile.is_open ( ) )
{
WriteOutputFile ( outFile, name, age, record_counter );
PrintTotalsAndSummary ( outFile, record_counter );
outFile.close ( );
}
else
{
cout << "Trouble Opening File: outFile";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
}
else
{
cout << "Trouble Opening File: inFile";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
return 0;
}
int ReadData ( ifstream & inFile, ofstream & outFile, char name[][MAX], int age[] )
{
int counter = 0;
inFile >> name[counter] >> age[counter]; // Priming Read
while ( !inFile.eof ( ) )
{
cout << setiosflags ( ios::left ) << setw ( 25 )
<< name[counter] << resetiosflags ( ios::left )
<< setw ( 4 ) << age[counter] << endl;
counter++;
inFile >> name[counter] >> age[counter];
}
return counter;
}
void WriteOutputFile ( ofstream &outFile, char name[][MAX], int age[], int counter )
{
outFile << " Here is the Output File" << endl;
for ( int r = 0; r < counter; r++ )
{
outFile << setiosflags ( ios::left ) << setw ( 25 )
<< name[r] << setw ( 4 )
<< resetiosflags ( ios::left ) << age[r]
<< endl;
}
}
void PrintTotalsAndSummary ( ofstream &outFile, int totalRecords )
{
// To screen
cout << "\n\n\t** Total Records: " << totalRecords << " **\n"
<< "\t\t The End \n";
// To file
outFile << "\n\n\t** Total Records: " << totalRecords << " **\n"
<< "\t\t The End \n";
}
RUN: ***output file is included in debug exercise folder in github***
tyler 27
taury 26
ollie 1
ryan 25
elayna 15
mom 40
** Total Records: 6 **
The End
================================================================================================
11.14 Programming Exercises
pp. 336-337
10 pts #1
Submit: code & run
CODE:
#include <iostream>
#include <fstream> // For the files!!!!
#include <iomanip> // For manipulators & formatting options
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::ios;
using std::resetiosflags;
using std::setiosflags;
using std::ifstream;
using std::ofstream;
const int EMPLOYEES = 20;
const int MAX = 21;
int ReadData ( ifstream &inFile, char name[][MAX], char lname [][MAX], char social[][MAX], float wage[],
float hours[], char status[]);
void PrintTotalsAndSummary (char name[][MAX], char lname [][MAX], char social[][MAX], float wage[],
float hours[], char status[], float pay[], float Opay[], float NetPay[], int record_counter);
int main ( )
{
char name[EMPLOYEES][MAX];
char lname[EMPLOYEES][MAX];
char social[EMPLOYEES][MAX];
float wage[EMPLOYEES];
float hours[EMPLOYEES];
float pay[EMPLOYEES];
float Opay[EMPLOYEES];
float NetPay[EMPLOYEES];
char status[EMPLOYEES];
int record_counter ( 0 );
ifstream inFile;
// Notice how this automatically opens the file
inFile.open ( "bus.txt");
if ( inFile.is_open ( ) )
{
record_counter = ReadData (inFile, name, lname, social, wage, hours, status);
inFile.close ( );
}
else
{
cout << "Trouble Opening File: inFile";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
PrintTotalsAndSummary (name, lname, social, wage, hours, status, pay, Opay, NetPay, record_counter);
return 0;
}
int ReadData ( ifstream & inFile, char name[][MAX], char lname [][MAX], char social[][MAX], float wage[],
float hours[], char status[])
{
int counter = 0;
inFile >> name[counter] >> lname[counter] >> social[counter] >> wage[counter] >> hours[counter] >> status[counter] ; // Priming Read
while ( !inFile.eof ( ) )
{
;
counter++;
inFile >> name[counter] >> lname[counter] >> social[counter] >> wage[counter] >> hours[counter] >> status[counter] ;
}
return counter;
}
void PrintTotalsAndSummary (char name[][MAX], char lname [][MAX], char social[][MAX], float wage[],
float hours[], char status[], float pay[], float Opay[], float NetPay[], int record_counter)
{
int i;
int reg_hours;
int o_hours;
float o_wage;
for (i = 0; i < record_counter; i++)
{
// overtime hours and pay
if (hours[i] > 40)
{
o_hours = hours[i] - 40;
o_wage = (wage[i] / 2) + wage[i];
Opay[i] = o_hours * o_wage;
pay[i] = wage[i] * 40;
NetPay[i] = pay[i] + Opay[i];
if (status[i] == 'F')
NetPay[i] -= 5.00;
}
// normal hours and pay
else
{
pay[i] = wage[i] * hours[i];
NetPay[i] = pay[i];
Opay[i] = 0;
if (status[i] == 'F')
NetPay[i] -= 5.00;
}
}
cout << setiosflags ( ios::left ) << setw (10)
<< "Name" << resetiosflags ( ios::left ) << setw(20)
<< "Last Name" << resetiosflags (ios::left) << setw(20)
<< "Social" << resetiosflags (ios::left) << setw(20)
<< "Wage" << resetiosflags (ios::left) << setw(20)
<< "Hours" << resetiosflags (ios::left) << setw(20)
<< "Regular Pay" << resetiosflags (ios::left) << setw(20)
<< "Overtime Pay" << resetiosflags (ios::left) << setw(20)
<< "Status" << resetiosflags (ios::left) << setw(20)
<< "Net Pay" << resetiosflags (ios::left) << endl;
cout << "===========================================================================================================================================================================" << endl;
for (int j = 0; j < record_counter; j++)
{
cout << setiosflags ( ios::left ) << setw (10)
<< name[j] << resetiosflags ( ios::left ) << setw(20)
<< lname[j] << resetiosflags (ios::left) << setw(20)
<< social[j] << resetiosflags (ios::left) << setw(20)
<< wage[j] << resetiosflags (ios::left) << setw(20)
<< hours[j] << resetiosflags (ios::left) << setw(20)
<< pay[j] << resetiosflags (ios::left) << setw(20)
<< Opay[j] << resetiosflags (ios::left) << setw(20)
<< status[j] << resetiosflags (ios::left) << setw(20)
<< NetPay[j] << resetiosflags (ios::left) << endl;
}
}
RUN:
till-t@Air:~/code/cst115-lab8-till-t/11.14_exercise|master⚡ ⇒ ./a.out
Name Last Name Social Wage Hours Regular Pay Overtime Pay Status Net Pay
===========================================================================================================================================================================
John Smith 123-09-8765 9 46 360 81 F 436
Molly Brown 432-89-7654 9.5 40 380 0 F 375
Tim Wheeler 239-34-3458 11.25 83 450 725.625 F 1170.62
Keil Wader 762-84-6543 6.5 35 227.5 0 P 227.5
Trish Dish 798-65-9844 7.52 40 300.8 0 P 300.8
Anthony Lei 934-43-9843 9.5 56 380 228 F 603
Kevin Ashes 765-94-7343 4.5 30 135 0 P 135
Cheryl Prince 983-54-9000 4.65 45 186 34.875 F 215.875
Kim Cares 343-11-2222 10 52 400 180 F 575
Dave Cockroach 356-98-1236 5.75 48 230 69 F 294
Will Kusick 232-45-2322 15 45 600 112.5 P 712.5
|