From 9d53d8857eaa1c9405894a88ca75bc4657e42f35 Mon Sep 17 00:00:00 2001 From: msglm Date: Sat, 14 Jan 2023 05:31:48 -0600 Subject: Inital Commit --- C++/Sorting Data/SortingData.cpp | 138 +++++++++++++++++++++++++++++++++++++++ C++/Sorting Data/fname.txt | 100 ++++++++++++++++++++++++++++ C++/Sorting Data/lname.txt | 100 ++++++++++++++++++++++++++++ 3 files changed, 338 insertions(+) create mode 100644 C++/Sorting Data/SortingData.cpp create mode 100644 C++/Sorting Data/fname.txt create mode 100644 C++/Sorting Data/lname.txt (limited to 'C++/Sorting Data') 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 +#include +#include +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 . + */ diff --git a/C++/Sorting Data/fname.txt b/C++/Sorting Data/fname.txt new file mode 100644 index 0000000..a72e2e2 --- /dev/null +++ b/C++/Sorting Data/fname.txt @@ -0,0 +1,100 @@ +James +Robert +John +Michae +David +Willia59 +Richar +Joseph +Thomas +Charle +Christ +Daniel +Matthe +Anthon +Mark +Donald +Steven +Paul +Andrew +Joshua +Kennet +Kevin +Brian +George +Timoth +Ronald +Edward +Jason +Jeffre +Ryan +Jacob +Gary +Nichol +Eric +Jonath +Stephe +Larry +Justin +Scott +Brando +Benjam +Samuel +Gregor +Alexan +Frank +Patric +Raymon +Jack +Dennis +Jerry +Tyler +Aaron +Jose +Adam +Nathan +Henry +Dougla +Zachar +Peter +Kyle +Ethan +Walter +Noah +Jeremy +Christ +Keith +Roger +Terry +Gerald +Harold +Sean +Austin +Carl +Arthur +Lawren +Dylan +Jesse +Jordan +Bryan +Billy +Joe +Bruce +Gabrie +Logan +Albert +Willie +Alan +Juan +Wayne +Elijah +Randy +Roy +Vincen +Ralph +Eugene +Russel +Bobby +Mason +Philip +Louis diff --git a/C++/Sorting Data/lname.txt b/C++/Sorting Data/lname.txt new file mode 100644 index 0000000..0767c24 --- /dev/null +++ b/C++/Sorting Data/lname.txt @@ -0,0 +1,100 @@ +Mary +Patricia +Jennifer +1 Linda +Elizabeth +0 Barbar +5 Susan +Jessica +Sarah 98 +5 Karen +4,437 Li +Nancy 96 +9 Betty +3 Margar334 +Sandra +Ashley +Kimberly +Emily 83 +Donna 82 +Michelle +6 Carol +Amanda +Dorothy +Melissa +0 Debora223 +Stephanie +Rebecca +Sharon + Laura +Cynthia +Kathleen +Amy 68 + Angela +Shirley + Anna + Brenda +Pamela +Emma 59 +Nicole + Helen + Samant626 +Katherine + Christ861 + Debra +Rachel + Caroly223 + Janet +Catherine +Maria 52 +Heather +Diane 51 +Ruth 51 +Julie 50 +Olivia +Joyce 49 +Virginia + Victor302 + Kelly +Lauren +Christina +Joan 46 +Evelyn +Judith +Megan 43 + Andrea +Cheryl +Hannah +Jacquelin410 +Martha +Gloria +Teresa +Ann 40 +Sara 40 +Madison + France109 +Kathryn +Janice +Jean 39 +Abigail +Alice 38 +Julia 37 +Judy 37 + Sophia +Grace 37 +Denise +Amber 37 +Doris 37 +Marilyn +Danielle +Beverly +Isabella +Theresa + Diana +Natalie +Brittany + Charlo576 +Marie 34 +Kayla 34 +Alexis + Lori -- cgit v1.2.3