diff options
author | msglm <msglm@techchud.xyz> | 2023-01-14 05:31:48 -0600 |
---|---|---|
committer | msglm <msglm@techchud.xyz> | 2023-01-14 05:31:48 -0600 |
commit | 9d53d8857eaa1c9405894a88ca75bc4657e42f35 (patch) | |
tree | eb1efc1d028b949dd83bb710c68be8eff58f26e7 /C++/Sorting Data/SortingData.cpp | |
download | school-code-master.tar.gz school-code-master.tar.bz2 school-code-master.zip |
Diffstat (limited to 'C++/Sorting Data/SortingData.cpp')
-rw-r--r-- | C++/Sorting Data/SortingData.cpp | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/C++/Sorting Data/SortingData.cpp b/C++/Sorting Data/SortingData.cpp new file mode 100644 index 0000000..252c4f5 --- /dev/null +++ b/C++/Sorting Data/SortingData.cpp @@ -0,0 +1,138 @@ +// Name: msglm +// Date: Sep 8th, 2022 +// Program Name: Sorting Data +// Description: Given a file of first and last names, sort them. + +#include <iostream> +#include <fstream> +#include <string> +using namespace std; + +//Constants +const int ARR_SIZE = 100; + +//This function takes reads a file from a +//location and turns it into a passed array +//The array and file are both passed by reference. +// +// +//Only if Stroustrup spent less time adding OOP to C +//and just made arrays and functions play nice +int read(string location, ifstream & fileVar, string (&array)[ARR_SIZE]) { + int epoch = 0; + fileVar.open(location); + + if (!fileVar) { + cout << "ERROR! " << location << " failed to open!"; + exit(1); //I am going to take the risky route and not use a return here + //Here is to hoping that Microsoft's MSVC is compliant with + //the standard C++ definition of 'exit' + // + //I have had troubles before since I use GCC + //MSVC is almost never in sync with other compilers + } + + while (getline(fileVar, array[epoch]) && fileVar.peek() != EOF) { + epoch++; + } + //Since this function has the true size of the array already, + //we can just return it for other functions use + return epoch; +} + +//Very bog standard bubble sort implementation +// +//This sorts an array that's passed by reference +// +//On another note, someone needs to make a better +//language for teaching algorithms +//they're ugly in code and horrendus in math notation +void sort(string (&array)[ARR_SIZE], int size) { + + string temp; + string * sortedArray = new string[ARR_SIZE]; + + //For every single position in an array + // + //This is what dictates how many "passes" on the array is done + for (int pos = 0; pos < size; pos++) { + //note a counter called compare position + //this counter, if it less than the size minus the current position in the array, then the counter will be added to + // + //This is where the actual comparision is done + for (int comparePos = 0; comparePos < size - pos; comparePos++) { + //so long as the compared position is less than the size of the array subtracted by the position + //check if the current position being compared is greater than the next position + //assign the currently compared position to temp, + //turn the current position into the next position + //then turn the next position in to the temp position + //this swaps array[comparePos] and array[comparePos + 1] + // + //This is where the actual swapping is done + if(array[comparePos] > array[comparePos + 1]) { + temp = array[comparePos]; + array[comparePos] = array[comparePos + 1]; + array[comparePos + 1] = temp; + } + } + } +} + + +int main() { + //Variable declaration + int choice; + string fname[ARR_SIZE]; + string lname[ARR_SIZE]; + string cname[ARR_SIZE]; + ifstream fnameFile; + ifstream lnameFile; + int fsize; + int lsize; + + //No input validation here since the rubric doesn't call for it. + cout << "1. Sort by First Name\n"; + cout << "2. Sort by Last Name\n"; + cout << "Enter Selection: "; + cin >> choice; + + //Read the files into an array and output the size of said array to a variable + fsize = read("fname.txt", fnameFile, fname); + lsize = read("lname.txt", lnameFile, lname); + + //Some cheap error checking since we have fsize and lsize + if (!fsize==lsize) { + cout << "ERROR: your first name and last name files do now have parity!"; + exit(1); + } + + //menu choices now matter + // + //I opt to go ahead and combine the names whe comparing since it's just easier to deal with + //Perhaps it would have been more efficent to do this in the "read" function, but it's a little late now + if (choice == 1) { + for(int epoch = 0; epoch < fsize+1; epoch++) { + cname[epoch] = fname[epoch] + " " + lname[epoch]; + } + sort(cname, fsize); + for(int epoch = 0; epoch < fsize+1; epoch++) { + cout << cname[epoch] << endl; + } + } else { + for(int epoch = 0; epoch < fsize+1; epoch++) { + cname[epoch] = lname[epoch] + " " + fname[epoch]; + } + sort(cname, fsize); + for(int epoch = 0; epoch < fsize+1; epoch++) { + cout << cname[epoch] << endl; + } + } +} + +/* + 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/>. + */ |