Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN C++: The text files babynames1880.txt and babynames2014.txt, which are attached to this assignment, contain a list of the most popular boy and girl names

IN C++:

The text files babynames1880.txt and babynames2014.txt, which are attached to this assignment, contain a list of the most popular boy and girl names in the United States for the years 1880 and 2014 as compiled by the Social Security Administration (its interesting to see how baby names have changed over the years!). If you want to see how names have evolved in popularity over time, check out http://www.babynamewizard.com/voyager (Links to an external site.)Links to an external site.. You will see that while Emma seems like a timeless female name if you just look at the babynames1880.txt and babynames2014.txt files (3rd in 1880 and 1st in 2014), it in fact fell rapidly in popularity through 1900, reached its nadir in the 1960s, and then started regaining its popularity in the 1980s.

A baby name file is a space-delimited file in which each line consists of a baby name, the names gender, and the number of babies given that name in the given birth year. The most popular names are listed first and the least popular names are listed last. For example, the file begins with:

Emma F 20799

Olivia F 19674

Noah M 19144

Sophia F 18490

Liam M 18342

Mason M 17092

Isabella F 16950

The first line indicates that Emma was the most popular girls name and was the name given to 20,799 babies. The third line indicates that Noah is the most popular boy name and was the name given to 19,144 babies.

In this part, you will write a program that reads names from a baby names file that is formatted as shown above and prints a nicely formatted report with the top n-ranked names and their frequencies. The user will provide 3 command line arguments1) the name of the baby names file, 2) the number n of top ranked names to print, 3) and the gender as a capital M or F (male or female)and then your program will print the top n ranked names for that gender. For example, if the user enters babynames2014.txt, 3, and F as the command line arguments, then your program will print the top ranked 3 female names as follows:

3 Most Popular Baby Names

Name Frequency

Emma 20791

Olivia 19674

Sophia 18490

Here are the formatting requirements for the report:

The names are left-justified in a field 10 characters wide.

The frequencies are right-justified in a field 10 characters wide (the length of the string Frequency).

Do not put any spaces between the Name and Frequency fields

Programming note: If the user requests a larger number of names than are in the baby names file, stop printing your report after you have exhausted all the names in the file. For example, if there are 10,000 names in the file, and the user asks for 20,000 names, then print 10,000 names and stop.

Here are several important requirements for this part:

You may not assume that the name of the input file is babynames2014.txt. We will test your code by providing other files with baby names via the command line.

You will need to convert the command line argument that represents the number of names to print from a string to an integer. You may use the atoi (). function for this.

Check to make sure that the user entered exactly 3 arguments. Print the following two line message if they did not:

Wrong number of arguments Correct usage: filename #namesToPrint gender 

After printing the usage message, return 0 from main to exit your program.

Remember that the name of the program is added by the operating system as the first argument (argv[0]) , and that therefore you program will receive a total of 4 arguments. Hence you will check to see if argc is 4, not 3. You MUST check argc before ever using argv!

Make sure that the file can be opened. If not, output an error message to stdout with the file name in order to print the error message and then return 0 from main to exit your program.

Hint: You do NOT need to use arrays for the babynames in Part 1.

Part 2 producing a more informative report

In this part you are going to modify the report that your program prints so that it splits the baby names by gender. For example, if the user wants the top 5 baby names, your report will now look like:

5 Most Popular Boys and Girls Names

Girls Frequency Boys Frequency

Emma 20799 Noah 19144

Olivia 19674 Liam 18342

Sophia 18490 Mason 17092

Isabella 16950 Jacob 16712

Ava 15586 William 16687

Here are the formatting instructions:

Use the same formatting for the names and frequencies as specified in part A.

Put 4 blank spaces between the column for Girls Frequency and the column for Boys names.

To produce this report, modify your program from part A so that it loads the entire baby names file and splits the names by gender into a girl array and into a boy array. Your program must also read the name frequencies into two more arrays, one for boys and one for girls.

Next, write a function named printTopNames that prints the report. Move your printing code from part A into this function and then modify it. printTopNames must be a void function that takes 7 parameters: the four arrays (pass the girl and boy name arrays and their corresponding frequency arrays by-reference) as well as the number girls and number of boys along with the number of top names to print. Remember to use const if you are not going to change the arrays. Your function will then print the top names report. If the user requests more names than are available, you must stop printing when you exhaust the array. Therefore, the shortest arrays size will be the number of names you print.

As in Part 1 the number of command line arguments provided by the user must exactly match the number that the program expects or you should print an error message. Since gender is no longer a command line argument, change the error message for an incorrect number of command arguments so that it reads:

Wrong number of arguments Correct usage: filename #namesToPrint 

Part 3 Output to a File

For this part of the program instead of putting the output to the stdout the program will output to an output file. To do this you will need to think about the following changes.

The command line arguments will now be inputfile outputfile followed by the number of names to print. You will need to readjust the code to accommodate this. The error messages will be as:

Wrong number of arguments Correct usage: ifilename ofilename #namesToPrint 

The output file must be opened and closed in the main program and then passed as an additional parameter to the printTopNames function

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands On Database

Authors: Steve Conger

1st Edition

013610827X, 978-0136108276

More Books

Students also viewed these Databases questions

Question

1-4 How will MIS help my career?

Answered: 1 week ago