aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Schroeder <[email protected]>2021-11-18 12:53:45 -0800
committerBenjamin Schroeder <[email protected]>2021-11-18 12:53:45 -0800
commit21caf78e9f0ec94dbb37e242cc8be3e6ee9b1734 (patch)
tree5348da689d29b3df2242df73617e5f899ef87edd
parentAdd online IDE url (diff)
downloadcst115-lab8-bensprogramma-21caf78e9f0ec94dbb37e242cc8be3e6ee9b1734.tar.xz
cst115-lab8-bensprogramma-21caf78e9f0ec94dbb37e242cc8be3e6ee9b1734.zip
11.7, 11.9, 11.13, 11.14
-rw-r--r--CST116F2021-Lab8/11_13_Average_Data.txt13
-rw-r--r--CST116F2021-Lab8/11_14_Data.txt11
-rw-r--r--CST116F2021-Lab8/CST116F2021-Lab8.cpp20
-rw-r--r--CST116F2021-Lab8/CST116F2021-Lab8.vcxproj2
-rw-r--r--CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters2
-rw-r--r--CST116F2021-Lab8/CST116F2021-Lab8_Schroeder.cpp376
-rw-r--r--CST116F2021-Lab8/Chap_11_Data.txt6
-rw-r--r--CST116F2021-Lab8/Chap_11_Report.txt1
-rw-r--r--CST116F2021-Lab8/LAB_8_CODE_RUN and Answers for 11.7.txt193
9 files changed, 602 insertions, 22 deletions
diff --git a/CST116F2021-Lab8/11_13_Average_Data.txt b/CST116F2021-Lab8/11_13_Average_Data.txt
new file mode 100644
index 0000000..9cd3abb
--- /dev/null
+++ b/CST116F2021-Lab8/11_13_Average_Data.txt
@@ -0,0 +1,13 @@
+15
+25
+12
+14
+26
+32
+52
+12
+27
+44
+2
+32
+
diff --git a/CST116F2021-Lab8/11_14_Data.txt b/CST116F2021-Lab8/11_14_Data.txt
new file mode 100644
index 0000000..856de78
--- /dev/null
+++ b/CST116F2021-Lab8/11_14_Data.txt
@@ -0,0 +1,11 @@
+John Smith 123-09-9765 9.00 46 F
+Molly Brown 432-89-7654 9.50 40 F
+Tim Wheeler 239-34-3458 11.25 83 F
+Keil Wader 762-84-6543 6.50 35 P
+Trish Dish 798-65-9844 7.52 40 P
+Anthony Lei 934-43-9843 9.5 56 F
+Kevin Ashes 765-94-7343 4.5 30 P
+Cheryl Prince 983-54-9000 4.65 45 F
+Kim Cares 343-11-2222 10.00 52 F
+Dave Cockroach 356-98-1236 5.75 48 F
+Will Kusick 232-45-2322 15.00 45 P \ No newline at end of file
diff --git a/CST116F2021-Lab8/CST116F2021-Lab8.cpp b/CST116F2021-Lab8/CST116F2021-Lab8.cpp
deleted file mode 100644
index d4a77b8..0000000
--- a/CST116F2021-Lab8/CST116F2021-Lab8.cpp
+++ /dev/null
@@ -1,20 +0,0 @@
-// CST116F2021-Lab8.cpp : This file contains the 'main' function. Program execution begins and ends there.
-//
-
-#include <iostream>
-
-int main()
-{
- std::cout << "Hello World!\n";
-}
-
-// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
-// Debug program: F5 or Debug > Start Debugging menu
-
-// Tips for Getting Started:
-// 1. Use the Solution Explorer window to add/manage files
-// 2. Use the Team Explorer window to connect to source control
-// 3. Use the Output window to see build output and other messages
-// 4. Use the Error List window to view errors
-// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
-// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
diff --git a/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj b/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj
index 00a34aa..ee055fd 100644
--- a/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj
+++ b/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj
@@ -139,7 +139,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
- <ClCompile Include="CST116F2021-Lab8.cpp" />
+ <ClCompile Include="CST116F2021-Lab8_Schroeder.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
diff --git a/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters b/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters
index b0158d2..599ecf8 100644
--- a/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters
+++ b/CST116F2021-Lab8/CST116F2021-Lab8.vcxproj.filters
@@ -15,7 +15,7 @@
</Filter>
</ItemGroup>
<ItemGroup>
- <ClCompile Include="CST116F2021-Lab8.cpp">
+ <ClCompile Include="CST116F2021-Lab8_Schroeder.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
diff --git a/CST116F2021-Lab8/CST116F2021-Lab8_Schroeder.cpp b/CST116F2021-Lab8/CST116F2021-Lab8_Schroeder.cpp
new file mode 100644
index 0000000..bdab3ec
--- /dev/null
+++ b/CST116F2021-Lab8/CST116F2021-Lab8_Schroeder.cpp
@@ -0,0 +1,376 @@
+// CST116F2021-Lab8_Schroeder.cpp : This file contains the 'main' function. Program execution begins and ends there.
+
+// ////////////////// 11.7 Exercises SHOWN IN LAB_8_CODE_RUN .txt FILE /////////////////
+
+// ////////////////////// 11.9 Learn By Doing Exercise pp323 ////////////////////////
+/*
+#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";
+}
+
+*/
+
+
+// /////////////////////// 11.13 Debugging Exercises pp 333 - 336 /////////////////////
+
+
+// ////////////////////// 11.14 Programming Exercise pp336 - 337 //////////////////
+
+
+
+
+
+
+
+
+
+
+
+
+
+// //////////////////// In Class example ////////////////////////////////////
+ // R-WFiles.cpp using getline
+ /*
+ int main()
+ {
+ /*
+ string records[RECORDS][MAX]; //2 dimensional string array
+ int num_records = 0;
+ ifstream inFile;
+ ofstream outFile;
+ inFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Data.txt"); // open the input file
+ outFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Report.txt"); // open the output file
+
+ if (inFile.is_open() && outFile.is_open()) // LINE BY LINE MANIPULATION
+ {
+ while (getline(inFile, records[num_records][0], ' ')) //reads the first item in the file
+ {
+ cout << records[num_records][0] << ' '; //print out to screen
+ outFile << records[num_records][0] << ' '; //print to the output file
+ getline(inFile, records[num_records][1], ' ');
+ cout << records[num_records][1] << ' '; //print out to screen
+ outFile << records[num_records][1] << ' '; //print to the output file
+ cout << records[num_records][2] << ' '; //print out to screen
+ outFile << records[num_records][2] << ' '; //print to the output file
+ getline(inFile, records[num_records][3], ' ');
+ cout << records[num_records][3] << ' '; //print out to screen
+ outFile << records[num_records][3] << ' '; //print to the output file
+ num_records++;
+
+
+ }
+
+ }
+ */
+ // R-WFiles.cpp using fscanf
+ /*
+ string records[RECORDS][MAX]{}; //2 dimensional string array
+ int num_records = 0;
+ char inChar[50];
+ string inString;
+
+ FILE* inPtr; // file pointer
+ ifstream inFile;
+ ofstream outFile;
+ //inFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Data.txt"); // open the input file
+ fopen_s(&inPtr, "C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Data.txt","r");
+ outFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\Chap_11_Report.txt"); // open the output file
+
+
+ if (inPtr != NULL && outFile.is_open()) //check that there is stuff to read // SAFER WAY TO READ THINGS IN fscan
+ {
+ while (fscanf_s(inPtr,"%s", inChar, 49) >=0) //reads the first item in the file, tells how big the space is. left space for null character
+ {
+ //inString = inChar; // convert it to a string and inserts the NULL character
+ records[num_records][0] = inChar;
+ cout << records[num_records][0] << ' '; //print out to screen
+ outFile << records[num_records][0] << ' '; //print to the output file
+
+ fscanf_s(inPtr,"%s", inChar, 49);
+ //inString = inChar; // convert it to a string and inserts the NULL character
+ records[num_records][1] = inChar;
+ cout << records[num_records][1] << ' '; //print out to screen
+ outFile << records[num_records][1] << ' '; //print to the output file
+
+ fscanf_s(inPtr,"%s", inChar, 49);
+ //inString = inChar; // convert it to a string and inserts the NULL character
+ records[num_records][2] = inChar;
+ cout << records[num_records][2] << ' '; //print out to screen
+ outFile << records[num_records][2] << ' '; //print to the output file
+
+ fscanf_s(inPtr,"%s", inChar, 49);
+ //inString = inChar; // convert it to a string and inserts the NULL character
+ records[num_records][3] = inChar;
+ cout << records[num_records][3] << ' '; //print out to screen
+ outFile << records[num_records][3] << ' '; //print to the output file
+
+ num_records++;
+ }
+
+ }
+
+ fclose(inPtr); // close input file
+ outFile.close(); // close output file
+
+
+ return 0;
+
+ }
+ */
+ // R-WFiles.cpp : fscanf_s reading strings
+ /*
+ #include <iostream>
+ #include <fstream>
+ #include <iomanip>
+ #include <string>
+ #include <stdio.h>
+ using namespace std;
+ const int RECORDS = 100;
+ const int MAX = 4;
+ int main()
+ {
+ string records[RECORDS][MAX]{};
+ int num_records = 0;
+ ifstream inFile;
+ ofstream outFile;
+ outFile.open("C:\\TEMP\\Chap_11_Report.txt");
+ FILE* inPtr;
+ char inChar[50]{};
+ string inString;
+ if (fopen_s(&inPtr, "C:\\TEMP\\Chap_11_Data.txt", "r") == 0)
+ {
+ if (inPtr != NULL && outFile.is_open())
+ {
+ while (fscanf_s(inPtr, "%s", inChar, 49) >= 0)
+ {
+ //inString = inChar;
+ records[num_records][0] = inChar;
+ cout << records[num_records][0];
+ outFile << records[num_records][0];
+ fscanf_s(inPtr, "%s", inChar, 49);
+ //inString = inChar;
+ records[num_records][1] = inChar;
+ cout << records[num_records][1];
+ outFile << records[num_records][1];
+ fscanf_s(inPtr, "%s", inChar, 49);
+ //inString = inChar;
+ records[num_records][2] = inChar;
+ cout << records[num_records][2];
+ outFile << records[num_records][2];
+ fscanf_s(inPtr, "%s", inChar, 49);
+ //inString = inChar;
+ records[num_records][3] = inChar;
+ cout << records[num_records][3];
+ outFile << records[num_records][3];
+ num_records++;
+ }
+ }
+ }
+ outFile.close();
+ fclose(inPtr);
+ }
+ */
+ // R-WFiles.cpp : fscanf_s reading ints
+ /*
+ #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;
+ ofstream outFile;
+ outFile.open("C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\11_13_MedianReport.txt");
+ FILE* inPtr;
+ fopen_s(&inPtr,"C:\\Users\\Lenovo\\source\\repos\\Lab8_Schroeder\\11_13_Average_Data.txt","r");
+ int inInt;
+
+ if (inPtr != 0)
+ {
+ if (inPtr != NULL && outFile.is_open())
+ {
+ int j = 0;
+ while (fscanf_s(inPtr, "%i", &inInt) >= 0)
+ {
+ records[num_records] = inInt;
+ //cout << records[num_records]<< " ";
+ outFile << records[num_records]<< " ";
+ num_records++;
+ j++;
+ }
+ }
+ }
+ else
+ cout << "Error: The File can not be opened";
+
+
+ outFile.close();
+ fclose(inPtr);
+
+ cout << "given list:\n";
+ for (int r = 0; r < num_records; r++)
+ {
+ cout << records[r] << " \n";
+
+ }
+ // 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 << "\n sorted list:\n";
+ for (int r = 0; r < num_records; r++)
+ {
+ cout << records[r] << " \n";
+ }
+ }
+ */
+ // R-WFiles.cpp : getline ints
+ /*
+ #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;
+ ofstream outFile;
+ outFile.open("C:\\TEMP\\Chap_11_intReport.txt");
+ inFile.open("C:\\TEMP\\Chap_11_intData.txt");
+ string inString;
+ if (inFile.is_open() && outFile.is_open())
+ {
+ while (getline(inFile, inString, ','))
+ {
+ records[num_records] = stoi(inString); //string to int
+ outFile << records[num_records];
+ cout << records[num_records];
+ num_records++;
+ }
+ }
+
+ outFile.close();
+ inFile.close();
+ }
+
+ */
+ // R-WFiles.cpp : cin/cout reading files
+ /*
+ #include <iostream>
+ #include <fstream>
+ #include <iomanip>
+ #include <string>
+ using namespace std;
+ const int RECORDS = 100;
+ const int MAX = 4;
+ int main()
+ {
+ int records[RECORDS];
+ int num_records = 0;
+ ifstream inFile("C:\\TEMP\\Chap_11_intData.txt");
+ ofstream outFile("C:\\TEMP\\Chap_11_intReport.txt");
+ streambuf* cinbuf = cin.rdbuf();
+ streambuf* coutbuf = cout.rdbuf();
+ cin.rdbuf(inFile.rdbuf());
+ cout.rdbuf(outFile.rdbuf());
+ while (cin >> records[num_records])
+ {
+ cout << records[num_records] << ' ';
+ num_records++;
+ }
+ cin.rdbuf(cinbuf);
+ cout.rdbuf(coutbuf);
+ }
+ */
diff --git a/CST116F2021-Lab8/Chap_11_Data.txt b/CST116F2021-Lab8/Chap_11_Data.txt
new file mode 100644
index 0000000..fd0e134
--- /dev/null
+++ b/CST116F2021-Lab8/Chap_11_Data.txt
@@ -0,0 +1,6 @@
+Name1 Address1 Phone1 Bday1
+Name2 Address2 Phone2 Bday2
+Name3 Address3 Phone3 Bday3
+Name4 Address4 Phone4 Bday4
+Name5 Address5 Phone5 Bday5
+Name6 Address6 Phone6 Bday6
diff --git a/CST116F2021-Lab8/Chap_11_Report.txt b/CST116F2021-Lab8/Chap_11_Report.txt
new file mode 100644
index 0000000..3b198ef
--- /dev/null
+++ b/CST116F2021-Lab8/Chap_11_Report.txt
@@ -0,0 +1 @@
+Name1 Address1 Phone1 Bday1 Name2 Address2 Phone2 Bday2 Name3 Address3 Phone3 Bday3 Name4 Address4 Phone4 Bday4 Name5 Address5 Phone5 Bday5 Name6 Address6 Phone6 Bday6 \ No newline at end of file
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
+