In: Computer Science
In this lab you will get a chance to explore names w/ Vectors. 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:
while (true){ getline(cin, name); if (name.empty()){ break; } // Add to vector of First Names getline(cin, name); // Add to vector of Last Names // Print out First Name and Last Name ending with newline (endl) }
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.
Finally, you will print out the names sorted. Print a title above the sorted names:
cout << " --Sorted Names-- " << endl;
Below's a Sample Session :
david ruby ej smith arlene kova --Sorted Names-- arlene kova david ruby ej smith
Good Luck!
#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
using namespace std;
int main(){
bool done;
int minIndex;
string minValue,minValue1;
string firstName;
string lastName;
vector<string> myList;
vector<string> myList1;
done = false;
while(!done){
cout<<"Enter first Name: ";
getline(cin, firstName);
if (!firstName.empty()){
myList.push_back(firstName);
// Prompt user with "Enter Last Name: "
cout<<"Enter Last Name: ";
getline(cin, lastName);
// Add to vector of Last Names
myList1.push_back(lastName);
// Print out First Name and Last Name ending with newline
(endl)
cout<<firstName<<"
"<<lastName<<"\n"<<endl;
}
else{
done = true;
}
}
for (int i=0; i<myList.size(); ++i){
minValue = myList.at(i);
minValue1=myList1.at(i);
minIndex = i;
for (int j=i; j<myList.size(); ++j){
if (myList.at(j) < minValue){
minIndex = j;
minValue = myList.at(j);
minValue1=myList1.at(j);
}
}
myList.at(minIndex) = myList.at(i);
myList.at(i) = minValue;
myList1.at(minIndex)=myList1.at(i);
myList1.at(i)=minValue1;
}
cout<<"\n-------------\nSorted
List\n-------------"<<endl;
for(int i=0;i<myList.size();++i){
cout<<myList[i]<<"
"<<myList1[i]<<endl;
}
return 0;
}
Output Screenshot:-
-------------------------------------------------
Please give me a UPVOTE. Thank you :)