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

The text file babynames2012.txt, which is included in the source code for this b

ID: 3837952 • Letter: T

Question

The text file babynames2012.txt, which is included in the source code for this book and is available online from the book’s Web site, contains a list of the 1000 most popular boy and girl names in the United States for the year 2012 as compiled by the Social Security Administration.
This is a space-delimited file of 1000 entries in which the rank is listed first, followed by the corresponding boy name and girl name. The most popular names are listed first and the least popular names are listed last. For example, the file begins with
1 Jacob Sophia
2 Mason Emma
3 Ethan IsabellaThis indicates that Jacob is the most popular boy name and Sophia is the most popular girl name. Mason is the second most popular boy name and Emma is the second most popular girl name.
Write a program that allows the user to input a name. The program should then read from the file and search for a matching name among the girls and boys. If a match is found, it should output the rank of the name. The program should also indicate if there is no match.
For example, if the user enters the name “Justice,” then the program should output:
Justice is ranked 519 in popularity among boys. Justice is ranked 518 in popularity among girls.
If the user enters the name “Walter,” then the program should output:
Walter is ranked 376 in popularity among boys. Walter is not ranked among the top 1000 girl names.

in C++

Explanation / Answer

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
vector<string> boyNames;
vector<string> girlNames;
ifstream nameFile("babynames2012.txt");

int rank;
string boyName, girlName;
while(nameFile >> rank >> boyName >> girlName)
{
boyNames.push_back(boyName);
girlNames.push_back(girlName);
}
nameFile.close();

cout << "Enter a name to search its rank: ";
string name;
cin >> name;

vector<string>::iterator it;

it=find(boyNames.begin(),boyNames.end(),name);
if(it != boyNames.end())
{
int pos = it - boyNames.begin();
cout << name << " is ranked " << pos << " in popularity among boys.";
}
else
{
cout << name << " is not ranked among the top 1000 boys.";
}

it=find(girlNames.begin(),girlNames.end(),name);
if(it != girlNames.end())
{
int pos = it - girlNames.begin();
cout << name << " is ranked " << pos << " in popularity among girls";
}
else
{
cout << name << " is not ranked among the top 1000 girl.";
}
cout << endl;

return 0;
}

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