diff options
| author | Benjamin Schroeder <[email protected]> | 2021-11-18 12:53:45 -0800 |
|---|---|---|
| committer | Benjamin Schroeder <[email protected]> | 2021-11-18 12:53:45 -0800 |
| commit | 21caf78e9f0ec94dbb37e242cc8be3e6ee9b1734 (patch) | |
| tree | 5348da689d29b3df2242df73617e5f899ef87edd /CST116F2021-Lab8/LAB_8_CODE_RUN and Answers for 11.7.txt | |
| parent | Add online IDE url (diff) | |
| download | cst115-lab8-bensprogramma-21caf78e9f0ec94dbb37e242cc8be3e6ee9b1734.tar.xz cst115-lab8-bensprogramma-21caf78e9f0ec94dbb37e242cc8be3e6ee9b1734.zip | |
11.7, 11.9, 11.13, 11.14
Diffstat (limited to 'CST116F2021-Lab8/LAB_8_CODE_RUN and Answers for 11.7.txt')
| -rw-r--r-- | CST116F2021-Lab8/LAB_8_CODE_RUN and Answers for 11.7.txt | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/CST116F2021-Lab8/LAB_8_CODE_RUN and Answers for 11.7.txt b/CST116F2021-Lab8/LAB_8_CODE_RUN and Answers for 11.7.txt new file mode 100644 index 0000000..1ba962d --- /dev/null +++ b/CST116F2021-Lab8/LAB_8_CODE_RUN and Answers for 11.7.txt @@ -0,0 +1,193 @@ +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 + |