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
|
//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:
RUN:
|