Advertisement
clairec

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

Apr 2nd, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. /* Sorts a set of names, first by last name, then, if necessary, by first name.
  2. If this has somehow saved your grade (you filthy cheater, you should be ashamed of yourself), then
  3. I'm glad I could be of help.
  4.  
  5. <3 Your Benevolent Goddess, Lady Claire */
  6.  
  7. #include<iostream>
  8. #include<vector>
  9. #include<string>
  10. using namespace std;
  11.  
  12. int main() {
  13.     int i,j,minIndex = 0;
  14.     bool done = false;
  15.     vector<string> firstNamesList,lastNamesList;
  16.     string userFirstName,userLastName,minValue1,minValue2;
  17.  
  18.     while (!done) {
  19.         cout << "Enter First Name: ";
  20.         getline(cin, userFirstName);
  21.         if (!userFirstName.empty()) {
  22.             firstNamesList.push_back(userFirstName);    
  23.  
  24.             cout << "Enter Last Name: ";
  25.             getline(cin, userLastName);
  26.             lastNamesList.push_back(userLastName);
  27.  
  28.             cout << userFirstName << " " << userLastName << endl;
  29.         }
  30.         else {
  31.             done = true;
  32.         }
  33.     }
  34.    
  35.     /* Initial sort of first and last names */
  36.  
  37.     for (i = 0; i < lastNamesList.size(); i++) {
  38.         minValue1 = lastNamesList.at(i);
  39.         minValue2 = firstNamesList.at(i);
  40.         minIndex = i;
  41.         for (j = i; j < lastNamesList.size(); j++) {
  42.             if (lastNamesList.at(j) < minValue1) {
  43.                 minIndex = j;
  44.                 minValue1 = lastNamesList.at(j);
  45.                 minValue2 = firstNamesList.at(j);
  46.             }
  47.         }
  48.          lastNamesList.at(minIndex) = lastNamesList.at(i);
  49.          lastNamesList.at(i) = minValue1;
  50.          firstNamesList.at(minIndex) = firstNamesList.at(i);
  51.          firstNamesList.at(i) = minValue2;
  52.        
  53.     }
  54.    
  55.     /* Sort any names with the same surname by first name as well */
  56.  
  57.     for (i = 0; i < firstNamesList.size(); i++) {
  58.         minValue1 = firstNamesList.at(i);
  59.         minIndex = i;
  60.         for (j = i; j < firstNamesList.size(); j++) {
  61.             if (lastNamesList.at(j) == lastNamesList.at(i)) {
  62.                 if (firstNamesList.at(j) < firstNamesList.at(i)) {
  63.                     minValue1 = firstNamesList.at(j);
  64.                     minIndex = j;
  65.                 }
  66.             }
  67.         }
  68.         firstNamesList.at(minIndex) = firstNamesList.at(i);
  69.         firstNamesList.at(i) = minValue1;
  70.     }
  71.  
  72.     cout << endl << " --Sorted Names-- " << endl;
  73.  
  74.     for (i = 0; i < lastNamesList.size(); i++) {
  75.         cout << firstNamesList.at(i) << " " << lastNamesList.at(i) << endl;
  76.     }
  77.    
  78.     return 0;
  79. }
  80.  
  81. /* CCC, 2016-04-01T23:29, Under the WTFPL */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement