Question

In: Computer Science

In this lab you will get a chance to explore names w/ Vectors. You will need...

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 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.

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!

Solutions

Expert Solution

#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 :)


Related Solutions

The Calendar Program The purpose of this lab is to give you a chance to use...
The Calendar Program The purpose of this lab is to give you a chance to use some of the data stream tools we have been discussing in a simple application. The assignment is to write a calendar application which allows the user to select a date, and either retrieve a previously stored calendar entry, or save a calendar entry. Your program should present a GUI interface which allows the user to specify the month, day, and year of the calendar...
Determine whether or not W is a subspace of R3 where W consists of all vectors...
Determine whether or not W is a subspace of R3 where W consists of all vectors (a,b,c) in R3 such that 1. a =3b 2. a <= b <= c 3. ab=0 4. a+b+c=0 5. b=a2 6. a=2b=3c
Using the Capacitor Lab simulation, you will explore the factors that affect the capacitance of a...
Using the Capacitor Lab simulation, you will explore the factors that affect the capacitance of a parallel plate capacitor. Start the Capacitor Lab simulation. Play with the simulation to get familiar with the options provided. When ready to start the lab, click “Reset All.” You should be on the “Introduction” tab. Slide the button on the battery to 1.5 V. Select all meters except Voltmeter and Electric field Detector. Make sure both Plate Charge and Electric Field Lines button are...
Come on now, get a job. In this activity, you will select a country and explore...
Come on now, get a job. In this activity, you will select a country and explore the concepts of unemployment and the phases of the business cycle as it relates to the economic well being of your selected country. Locate a recent article (published within the last year) that addresses the unemployment theory and unemployment rate of your selected country. You can use the Hunt Library, newspapers, new stations, or other credible sources to locate an article. Analyze your selected...
You are off to shopping and need a program that allows you to enter the names...
You are off to shopping and need a program that allows you to enter the names of items, their price and quantities. Here is what a sample run should look like (with the keyboard input shown in italics) ... Enter item information ("exit" to exit) item 1: chips price: 3.5 quantity: 2 Enter item information ("exit" to exit) item 2: red wine price : 15.0 quantity : 1 Enter item information ("exit" to exit) item 3 : steaks price :...
In this lab, you will explore some basics of Solidwork. 1. Create the numbers 0 –...
In this lab, you will explore some basics of Solidwork. 1. Create the numbers 0 – 9 with lines and arcs 2. Create a polygon with 5 sides and with 9 sides 3. Create two lines with the interior angle between the lines of 145º 4. Create a centerline inside a 4-sided polygon 5. Create a circle of 5 inches in diameter and extrude it for 12 inches in height. Type your name on the surface and extrude cut it...
Lab Scenario: You have a 1 W light bulb in your lab. It puts out light...
Lab Scenario: You have a 1 W light bulb in your lab. It puts out light of only 1 frequency. The wavelength of this light is 500nm. you set up a detector with a surface area of 1 square centimeter facing the light source at a distance of 100m a) Find intensity of light due to your 1 W light bulb at the location of the detector. Answer in micro W/m^2. b) Find the number of photons hitting the detector...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Develop a GUI to get unique names from the user.
in JAVADevelop a GUI to get unique names from the user. . In the first frame use a label ("Name") a textfield and 2 buttons (ext' and 'inished") . In the second frame there should be a list and a scrollpane as the components . Use the components on the first frame to get the name value from the user. . When the user enters a name make sure it is not empty and has no spaces and dicks the next' button, reset the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT