aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab8/LAB_8_CODE_RUN and Answers for 11.7.txt
blob: 6b9c855f47422876bdfbc8d895166b3abb9665a6 (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
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
455
456
457
458
459
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

////  //////////////////////////////    CODE  ///////////////////////////////////////////
*******************************************************************
* File: Chapter 11 Debug.cpp
*
* General Instructions: Complete each step before proceeding to the
* next.
*
* Debugging Exercise 1
*
* 1) Make your own data file like Troy 12, with the next person on the
*    next line and save it to a directory you create on your drive.
* 2) Under the Project menu, select Add Existing Item and
*    add the input file you just placed on your drive to your
*    current project. Make sure your Solution Explorer window
*    is visible. If not, you can display it by selecting Solution
*    Explorer (or Ctrl+Alt+L).
* 3) Open the input file by simply double clicking the name of the
*    file in your Solution Explorer.
* 4) Build and execute the program.
* 5) Update the file paths below to correctly represent the path you
*    created.
* 6) Rebuild and execute the program.
* 7) Examine the code and the output and notice the use of
*    parallel arrays.
* 8) Add the output file created via the execution of
*    your program to your Project.  Execute your program again
*    and notice how Visual Studio has rewritten your output file
*    and asks if you would like to reload the file (select Yes).
* 9) Examine the contents of both the input and the output file.
* 10) Fix all the problems in your code that exist in relation to
*     the output. Verify that your output is appropriate for your
      input file.
* 11) Build and execute your code until you have all errors
*     removed and all the output is correct.
*
* Debugging Exercise 2
*
* 1) Replace the double slashes (\\) in the input file open statement
*    with only a single slash
*    (i.e., inFile.open("C:\TEMP\Chap_11_data.txt").
* 2) Build your code, noticing the impact of the invalid path you
*    created in the previous step.
* 3) Replace the backslashes as they were.
* 4) Change both the input and output filenames so they are
*    invalid.
* 5) Update the file related error messages within the code
*    to also provide the specific name of the file that is having a
*    problem.
* 6) Rebuild and execute your program to verify that your messages
*    are correct.
* 7) Correct the path names.
* 8) Build and execute your code and carefully check your
*    output on both the console and in the output file.
*
********************************************************************/
/*
#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::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("Chap_11_Report2.txt");

    inFile.open("Chap_11_data2.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";
            cout << "\n\n\t\t ** About to EXIT NOW! ** ";
        }
    }
    else
    {
        cout << "Trouble Opening File";
        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   //////////////////////////////////

Troy                       12
George                      4
Kelly                      13


        ** Total Records: 3 **
                 The End

C:\Users\Lenovo\Source\Repos\cst115-lab8-BensProgramma\x64\Debug\CST116F2021-Lab8.exe (process 21864) 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 . . .


###############################################################################################################################
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 . . .