The text files USPopulaiNames2010 txtm, contains a list of the 1,000 most popula
ID: 3680653 • Letter: T
Question
The text files USPopulaiNames2010 txtm, contains a list of the 1,000 most popular boy and girl names in the United States for the year 2010 as compiled by the Social Security Administration. These are blank-delimited files where the most popular boys and girls name is listed first, the second most popular boys and girls name is listed second, and so on, to the 1,000^th most popular boys and girls name, which is listed last. Here is an example of one line: of the input file This indicates that the 30^th popular name for boys is Liam and for girls is Sarah Note the first line of the file contains a description of the data, so you really want to skip reading the first line of the input file. What you need to do: Write a program that will read the data from the files and store them into two single dimension array, one for boys and one for girls. Proceed to reading the whole data into the arrays prompt the user to input a name to search for. The program should search through the array(s). If there is a match then it should output the popularity ranking and whether it is a girl or boy name. The program should also indicate if there is no match. The program should then prompt the user if they which to search for another name or end the program. If yes to search then go back to step 3 else end the program.Explanation / Answer
#include <fstream>
#include<iostream>
using namespace std;
int search(string boys[], string girls[], string key, int n);
int main()
{
ifstream fin("USPopularNames2010.txt");
int index;
string boys[1000];
string girls[1000];
string title;
getline(fin, title); // reading first line, skipping
int i=0;
while (fin >> index >> boys[i] >> girls[i])
{
i++;
}
char c = 'y';
string key;
while(c=='y'){
cout<<"Enter name to search: ";
cin>>key;
int index = search(boys, girls, key, i);
if(index == -1)
cout<<"No Match ";
else
cout<<"Rank of "<<key<<" is: "<<index<<endl;
cout<<"Do you want to search more (y/n): ";
cin>>c;
}
return 0;
}
int search(string boys[], string girls[], string key, int n){
for(int i=0; i<n; i++){
if((key.compare(boys[i]) == 0) || (key.compare(girls[i]) == 0))
return i+1;
}
return -1; // not found
}
/*
Output:
input file:
index boys girls
1 pravesh prity
2 mk sk
3 as ds
4 xs cd
5 qw re
Enter name to search: pravesh
Rank of pravesh is: 1
Do you want to search more (y/n): y
Enter name to search: as
Rank of as is: 3
Do you want to search more (y/n): y
Enter name to search: xs
Rank of xs is: 4
Do you want to search more (y/n): n
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.