Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function that takes as parameters two vectors of strings, the first popu

ID: 3780347 • Letter: W

Question

Write a function that takes as parameters two vectors of strings, the first populated, the second being initially empty.

In the first parameter, some elements of the vector will be names in the format FirstName LastName (i.e. they consist of two names separated by a single space), while some elements will consist only of a first name.

The function will construct a second vector holding First Names only.

Names vector

Firstnames vector

Harry Potter

Harry

Adele

Adele

Frank Sinatra

Frank

Julia Roberts

Julia

This is a void function, to be called compileFirstnames; both parameters are passed by reference; use the const modifier as appropriate.

Finally, write a statement invoking the function, passing in previously declared vectors names (populated with names as indicated above), and firstNames (initially empty).

Names vector

Firstnames vector

Harry Potter

Harry

Adele

Adele

Frank Sinatra

Frank

Julia Roberts

Julia

Explanation / Answer

#include <iostream>
#include <vector>
using namespace std;
void compileFirstnames(vector<string> & names, vector<string> & firstNames){
for(int i=0; i<names.size(); i++){
string firstName = names[i].substr(0, names[i].find(" "));
firstNames.push_back(firstName);
}

}
int main()
{
vector<string> names;
vector<string> firstNames;
names.push_back("Harry Potter");
names.push_back("Adele");
names.push_back("Frank Sinatra");
names.push_back("Julia Roberts");
compileFirstnames(names, firstNames);
cout<<"Firstnames are: "<<endl;
for(int i=0; i<firstNames.size(); i++){
cout<<firstNames[i]<<endl;
}
return 0;
}

Output:

sh-4.2$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                                                   

sh-4.2$ main                                                                                                                                                                                                                                           

Firstnames are:                                                                                                                                                                                                                                        

Harry                                                                                                                                                                                                                                                  

Adele                                                                                                                                                                                                                                                  

Frank                                                                                                                                                                                                                                                  

Julia

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote