clairec icon

Sorted Names (Computer Sci. 40, Prof. Ruby, CSU Fresno)

clairec | PRO | 04/02/16 06:24:02 AM UTC | 0 ⭐ | 672 👁️ | Never ⏰ | []
C++ |

2.51 KB

|

None

|

0 👍

/

0 👎

/* Sorts a set of names, first by last name, then, if necessary, by first name.
If this has somehow saved your grade (you filthy cheater, you should be ashamed of yourself), then
I'm glad I could be of help.
 
<3 Your Benevolent Goddess, Lady Claire */
 
#include<iostream>
#include<vector>
#include<string>
using namespace std;
 
int main() {
    int i,j,minIndex = 0;
    bool done = false;
    vector<string> firstNamesList,lastNamesList;
    string userFirstName,userLastName,minValue1,minValue2;
 
    while (!done) {
        cout << "Enter First Name: ";
        getline(cin, userFirstName);
        if (!userFirstName.empty()) {
            firstNamesList.push_back(userFirstName);    
 
            cout << "Enter Last Name: ";
            getline(cin, userLastName);
            lastNamesList.push_back(userLastName);
 
            cout << userFirstName << " " << userLastName << endl;
        }
        else {
            done = true;
        }
    }
    
    /* Initial sort of first and last names */
 
    for (i = 0; i < lastNamesList.size(); i++) {
        minValue1 = lastNamesList.at(i);
        minValue2 = firstNamesList.at(i);
        minIndex = i;
        for (j = i; j < lastNamesList.size(); j++) {
            if (lastNamesList.at(j) < minValue1) {
                minIndex = j;
                minValue1 = lastNamesList.at(j);
                minValue2 = firstNamesList.at(j);
            }
        }
         lastNamesList.at(minIndex) = lastNamesList.at(i);
         lastNamesList.at(i) = minValue1;
         firstNamesList.at(minIndex) = firstNamesList.at(i);
         firstNamesList.at(i) = minValue2;
       
    }
    
    /* Sort any names with the same surname by first name as well */
 
    for (i = 0; i < firstNamesList.size(); i++) {
        minValue1 = firstNamesList.at(i);
        minIndex = i;
        for (j = i; j < firstNamesList.size(); j++) {
            if (lastNamesList.at(j) == lastNamesList.at(i)) {
                if (firstNamesList.at(j) < firstNamesList.at(i)) {
                    minValue1 = firstNamesList.at(j);
                    minIndex = j;
                }
            }
        }
        firstNamesList.at(minIndex) = firstNamesList.at(i);
        firstNamesList.at(i) = minValue1;
    }
 
    cout << endl << " --Sorted Names-- " << endl;
 
    for (i = 0; i < lastNamesList.size(); i++) {
        cout << firstNamesList.at(i) << " " << lastNamesList.at(i) << endl;
    }
    
    return 0;
}
 
/* CCC, 2016-04-01T23:29, Under the WTFPL */

Comments