logo CodeStepByStep logo


Language/Type: C++ basics streams file input
Related Links:
Author: Marty Stepp (on 2016/06/16)

Write a function named printMostCommonName that accepts as its parameter a string representing a file name for a file of input. The corresponding file contains data that is a sequence of first names separated by whitespace. Some names might occur more than once in the data; all occurrences of a given name will appear consecutively in the file. Your function's job is to print the name that occurs the most frequently in the file, along with how many times it occurs. You should also return the total number of unique names that were seen in the entire file.

If two or more names occur the same number of times, print the one that appears earlier in the file. If every name in the file is different, every name will have 1 occurrence, so you should just print the first name in the file. For example, if the file names1.txt contains the following text:

Eric   Eric  Marty
Kim  Kim Kim   Jenny  Nancy 
Nancy  Nancy  Paul  Paul

Then the call of printMostCommonName("names1.txt"); would produce the following output and should return 7, because there are 7 unique names in the file:

Most common name: Kim, 3

This is because in this data, there is one occurrence of the name Benson, two occurrences of Eric, one occurrence of Marty, three occurrences of Kim, one of Jenny, three of Nancy, and two of Paul. Kim and Nancy appear the most times (3), and Kim appears first in the file. So for that line, your function should print that the most common is Kim.

As a second example, if the file names2.txt contains the following text:

 Stuart   Stuart
Stuart Ethan Alyssa  Alyssa Helene   Jessica 
  Jessica Jessica    Jessica

Then the call of printMostCommonName("names2.txt"); would produce the following output and should return 5:

Most common name: Jessica, 4

As a third example, if the file names3.txt contains the following text:

Jared  Alisa 

Yuki   Catriona  
 Cody   Coral   Trent 
Kevin  Ben Stefanie Kenneth

Notice that in this input data, every name is unique. So the call of printMostCommonName("names3.txt"); would produce the following output and should return 11:

Most common name: Jared, 1

If the file does exist, you may assume that it contains at least one name. But notice that the names might be separated by multiple spaces or lines, and that some lines might be blank. Your code should process the data properly regardless of the spacing between tokens. Each name will be separated by at least one whitespace character. If the input file does not exist or is not readable, your function should print no output and should return 0.

Constraints: Your solution should read the file only once, not make multiple passes over the file data. You may use data structures in your solution if you like, but it is possible/intended to solve it without any data structures.

Type your solution here:

This is a function problem. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.