You will need to create two vectors of strings, one for first names, and one for
ID: 3678148 • Letter: Y
Question
You will need to create two vectors of strings, one for first names, and one for last names. You will then read in first and last names and place these into vectors.
See example below:
You will not know how many names will be entered, and will need to use vector functions to add strings into your vector.
After reading both the first name and last name, print the first name and last name separated by a space with a newline.
You will then sort the list of names using the selection sort we described in class. You will need to make some changes in order to sort on last name first, then first name. Below's basic loop for our Selection Sort :
Finally, you will print out the names sorted. Print a title above the sorted names:
The extra endline will help format the output since ZyLabs does not echo your inputs.
Below's a Sample Session :
Explanation / Answer
nclude <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
int main ()
{
int USER_NAMES;
vector<string> firstNames;
vector<string> lastNames;
string userFirst;
string userLast;
string minValue;
string temp;
char user = 'c';
int i;
int j;
int minIndex;
while(user != 'q')
{
cout << "Please enter First Name: ";
cin >> userFirst;
cout << userFirst << endl;
firstNames.push_back(userFirst);
cout << "Please enter Last Name: ";
cin >> userLast;
cout << userLast << endl;
lastNames.push_back(userLast);
cout << userFirst << " " << userLast << endl;
cout << "Would you like to continue " << endl;
cin >> user;
USER_NAMES = USER_NAMES + 1;
}
cout << endl << "The Sorted Names" << endl;
for( i = 0; i < USER_NAMES; ++i )
{
minValue = firstNames.at(0);
for(j = i; j < USER_NAMES; ++j)
{
if (firstNames.at(i) < minValue)
{
temp = j;
minValue = firstNames.at(i);
}
}
}
for(i = 0; i < USER_NAMES; ++i)
{
cout << firstNames.at(i) << " " << lastNames.at(i) << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.