summaryrefslogtreecommitdiffstats
path: root/C++/Sorting Data/SortingData.cpp
blob: 252c4f5002d480ace4cfba49c37ba3351f7dce62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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/>.
   */