blob: 21dfaa0c3de00b945c8ca6d03c2b7acc46f60695 (
plain) (
blame)
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
|
Lab 8 Exercises Benjamin Schroeder
######################################################################################################################
13a
11.7 Exercises
p 317
4 pts #5-6
Submit: Statements for 5, Judgements for 6
###########################################
#5
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
using std::ifstream;
using std::ofstream;
using std::ios;
const int RECORDS = 100;
const int MAX = 4;
int main()
/*
{
string records;
int num_records = 0;
ifstream input_file; //define input file
ofstream output_file; //define output file
char lname[21], id[5];
int age;
input_file.open("Sample.txt"); // open the input file
output_file.open("Report.txt"); // open the output file
if (input_file.is_open() && output_file.is_open())
{
input_file >> lname[num_records]<<id[num_records]<<age[num_records]; //priming read
while (!input_file.eof())
{
num_records++;
input_file>> lname[num_records]<<id[num_records]<<age[num_records];
}
input_file.close(); //close input file
else
cout<<"Error: Unable to open input_file." << endl;
}
input_file.close(); // close input file
output_file.close(); // close output file
return 0;
}
##############################################
#6
a) False
b) True
c) False
d) False
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 /////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <stdio.h>
using namespace std;
const int RECORDS = 100;
const int MAX = 4;
int main()
{
int records[RECORDS]{};
int num_records = 0;
ifstream inFile;
FILE* inPtr;
// open the file
fopen_s(&inPtr, "C:\\Users\\Lenovo\\source\\repos\\cst115-lab8-BensProgramma\\CST116F2021-Lab8\\11_13_Average_Data.txt", "r");
int inInt;
// read the file
if (inPtr != 0)
{
if (inPtr != NULL)
{
int j = 0;
while (fscanf_s(inPtr, "%i", &inInt) >= 0)
{
records[num_records] = inInt;
num_records++;
j++;
}
}
}
else
cout << "Error: The File can not be opened";
// close the file
fclose(inPtr);
cout << "Given List:\n";
for (int r = 0; r < num_records; r++)
{
cout << records[r] << " ";
}
// Sort the numbers smallest to largest
int j = 0, k = 0, temp = 0;
for (j = 0; j < num_records; j++)
{
for (k = 0; k < num_records - 1; k++)
{
if (records[k] > records[k + 1])
{
temp = records[k];
records[k] = records[k + 1];
records[k + 1] = temp;
}
}
}
cout << "\nSorted List:\n";
for (int r = 0; r < num_records; r++)
{
cout << records[r] << " ";
}
// Find the median of the list
float median = 0;
// if number of elements are even
if (num_records % 2 == 0)
median = (records[(num_records - 1) / 2] + records[num_records / 2]) / 2.0;
// if number of elements are odd
else
median = records[num_records / 2];
cout << "\nMedian: " << median << "\n";
}
//////////////////////////////////////////////// RUN /////////////////////////////////////////
Given List:
15 25 12 14 26 32 52 12 27 44 2 32
Sorted List:
2 12 12 14 15 25 26 27 32 32 44 52
Median: 25.5
C:\Users\Lenovo\Source\Repos\cst115-lab8-BensProgramma\x64\Debug\CST116F2021-Lab8.exe (process 21852) 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 . . .
##############################################################################################################################
13c
11.13 Debugging Exercises
pp 333-336
10 pts
Submit: code & runs
###############################################################################################################################
13d
11.14 Programming Exercises
pp. 336-337
10 pts #1
Submit: code & run
34 pts
// ///////////////////////// CODE //////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
char first[100]{};
char last[100]{};
char ssn[12]{};
float wage =0;
int hours = 0;
char status;
int count = 1;
int num_records = 0;
float dues = 0.00;
int OThours = 0;
float STPay = 0.00;
float OTPay = 0.00;
float NetPay = 0.00;
ifstream fin;
//fin.open("C:\\Users\\Lenovo\\source\\repos\\cst115-lab8-BensProgramma\\CST116F2021-Lab8\\11_14_Data.txt");
fin.open("11_14_Data.txt"); //open the file
cout << " Name\t\t SSN#\tWage\t Hours\tRegPay\t\t OT\t Status\tNet Pay\n" << endl; //Output Table Header
while (fin && !fin.eof())
{
fin >> first; // Read the file
fin >> last;
fin >> ssn;
fin >> wage;
fin >> hours;
fin >> status;
num_records++;
OThours = 0; //Calculate Overtime if needed
if (hours> 40)
{
OThours = hours - 40;
hours = hours - OThours;
}
STPay = wage * hours;
OTPay = wage * (1.5 * OThours);
hours = hours + OThours;
dues = 5.00; //Calculate Union Dues
if (status=='P')
{
dues = 0.00;
}
NetPay = STPay + OTPay - dues; //Calculate Net Pay
if ( num_records<12) //Print out Table
{
cout << first << " "
<< last << "\t"
<< ssn << "\t$"
<< fixed << setprecision(2)<< wage << "\t\t"
<< hours << "\t$"
<< fixed << setprecision(2) << STPay << "\t\t$"
<< fixed << setprecision(2) << OTPay << "\t\t"
<< status << "\t"
<< "$" << fixed << setprecision(2) << NetPay << endl;
count++;
}
}
fin.close(); //Close the file
}
// ///////////////////////////////// RUN ////////////////////////
Name SSN# Wage Hours RegPay OT Status Net Pay
John Smith 123-09-9765 $9.00 46 $360.00 $81.00 F $436.00
Molly Brown 432-89-7654 $9.50 40 $380.00 $0.00 F $375.00
Tim Wheeler 239-34-3458 $11.25 83 $450.00 $725.62 F $1170.62
Keil Wader 762-84-6543 $6.50 35 $227.50 $0.00 P $227.50
Trish Dish 798-65-9844 $7.52 40 $300.80 $0.00 P $300.80
Anthony Lei 934-43-9843 $9.50 56 $380.00 $228.00 F $603.00
Kevin Ashes 765-94-7343 $4.50 30 $135.00 $0.00 P $135.00
Cheryl Prince 983-54-9000 $4.65 45 $186.00 $34.88 F $215.88
Kim Cares 343-11-2222 $10.00 52 $400.00 $180.00 F $575.00
Dave Cockroach 356-98-1236 $5.75 48 $230.00 $69.00 F $294.00
Will Kusick 232-45-2322 $15.00 45 $600.00 $112.50 P $712.50
C:\Users\Lenovo\Source\Repos\cst115-lab8-BensProgramma\x64\Debug\CST116F2021-Lab8.exe (process 9572) 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 . . .
|