summaryrefslogtreecommitdiffstats
path: root/C++/Vectors/Vectors.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'C++/Vectors/Vectors.cpp')
-rw-r--r--C++/Vectors/Vectors.cpp139
1 files changed, 139 insertions, 0 deletions
diff --git a/C++/Vectors/Vectors.cpp b/C++/Vectors/Vectors.cpp
new file mode 100644
index 0000000..67adaae
--- /dev/null
+++ b/C++/Vectors/Vectors.cpp
@@ -0,0 +1,139 @@
+// Name: msglm
+// Date: Sep 8th, 2022
+// Program Name: Vectirs
+// Description: Read in a file, output the data to the screen and the student who has the highest credit hours and highest GPA.
+
+#include <iostream>
+#include <fstream>
+#include <string>
+#include <vector>
+#include <array>
+#include <iomanip>
+using namespace std;
+
+//initalizes the file
+void initalize(string location, ifstream & fileVar) {
+ fileVar.open(location);
+
+ if (!fileVar) {
+ cout << "ERROR! " << location << " failed to open!";
+ exit(1);
+ }
+}
+
+//Takes in the initalized file and turns it into a vector of arrays of strings
+vector<array<string, 10>> serializeIntoVecArr(ifstream & fileVar) {
+ vector<array<string, 10>> studentDataContainer;
+ studentDataContainer.push_back(array<string, 10>());
+ int student = 0;
+ int data = 0;
+
+ while(getline(fileVar, studentDataContainer[student][data]) && fileVar.peek() != EOF) {
+ data = data + 1;
+ if (data == 10) {
+ data = 0;
+ student = student + 1;
+ //This initializes the next set of arrays
+ studentDataContainer.push_back(array<string, 10>());
+ }
+ }
+ return studentDataContainer;
+}
+
+
+//Prints out all the data in a tabular format
+void allData(vector<array<string, 10>> studentDataContainer) {
+
+ //this code repetition is gross
+ //Perhaps I could have made this cleaner
+ cout << left << setw(25) << "First Name" << " ";
+ cout << left << setw(25) << "Last Name" << " ";
+ cout << left << setw(25) << "T-Number" << " ";
+ cout << left << setw(25) << "Major" << " ";
+ cout << left << setw(25) << "Advisor" << " ";
+ cout << left << setw(25) << "GPA" << " ";
+ cout << left << setw(25) << "Credit Hours" << " ";
+ cout << left << setw(25) << "Years until Graduation" << " ";
+ cout << left << setw(25) << "Hardest Class" << " ";
+ cout << left << setw(25) << "Easiest Class" << " ";
+
+ //C++ 11 range based loops
+ //I wish I knew about these earlier
+ for (array<string, 10> student : studentDataContainer) {
+ cout << endl;
+ for (string data : student) {
+ cout << left << setw(25) << data << " ";
+ }
+ }
+
+}
+
+
+
+void highestCreditHours(vector<array<string, 10>> studentDataContainer) {
+ cout << "\n\nStudent with highest credit hours:\n";
+ int highestStudent = 0;
+ int highest = 0;
+
+ //gets the largest using only one pass through
+ for(int studentPos = 0; studentPos < studentDataContainer.size(); studentPos++) {
+ for (string data : studentDataContainer[studentPos]) {
+ if (data[6] > highest) {
+ highest = data[6];
+ highestStudent = studentPos;
+ }
+ }
+ }
+
+ //Outputs all the data of the largest
+ for (int dataPos = 0; dataPos < 10; dataPos++) {
+ cout << left << setw(25) << studentDataContainer[highestStudent][dataPos] << " ";
+ }
+}
+
+//clone of highestCreditHours
+void highestGPA(vector<array<string, 10>> studentDataContainer) {
+ cout << "\n\nStudent with highest GPA \n";
+ int highestStudent = 0;
+ int highest = 0;
+
+ for(int studentPos = 0; studentPos < studentDataContainer.size(); studentPos++) {
+ for (string data : studentDataContainer[studentPos]) {
+ if (data[7] > highest) {
+ highest = data[7];
+ highestStudent = studentPos;
+ }
+ }
+ }
+
+ for (int dataPos = 0; dataPos < 10; dataPos++) {
+ cout << left << setw(25) << studentDataContainer[highestStudent][dataPos] << " ";
+ }
+
+}
+
+int main() {
+ //Variable declaration
+ ifstream data;
+
+
+ //"Read in an unknown number of records from an input file and store that data into vector variables for the below"
+ //
+ //The data will be going into a vector, but that vector will hold arrays at a fixed size since each student's data will be fixed
+ //This makes accessing the data infinitely easier
+ initalize("input.txt", data);
+ vector<array<string, 10>> serializedVectorArray = serializeIntoVecArr(data);
+
+ allData(serializedVectorArray);
+ highestCreditHours(serializedVectorArray);
+ highestGPA(serializedVectorArray);
+
+}
+
+/*
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 ONLY as published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */