aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab8/CST116F2021-Lab8_Schroeder.cpp
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 /CST116F2021-Lab8/CST116F2021-Lab8_Schroeder.cpp
parentAdd online IDE url (diff)
downloadcst115-lab8-bensprogramma-21caf78e9f0ec94dbb37e242cc8be3e6ee9b1734.tar.xz
cst115-lab8-bensprogramma-21caf78e9f0ec94dbb37e242cc8be3e6ee9b1734.zip
11.7, 11.9, 11.13, 11.14
Diffstat (limited to 'CST116F2021-Lab8/CST116F2021-Lab8_Schroeder.cpp')
-rw-r--r--CST116F2021-Lab8/CST116F2021-Lab8_Schroeder.cpp376
1 files changed, 376 insertions, 0 deletions
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);
+ }
+ */