logo CodeStepByStep logo

biggestFamily

Related Links:
Author: Marty Stepp (on 2016/08/27)

Write a function named biggestFamily that reads an input file of people's names and prints information about which family has the most people in it. Your function accepts a string parameter representing a filename of input.

The input file contains a collection of names, one per line, in the format of the example shown at right. Each line of the file contains a first name (given name), a single space, and a last name (surname / family name). For example, in the name "Ned Stark", the word "Ned" is the first name and "Stark" is the last name. You may assume that every line follows this exact format and that first and last names are single words.

Jon Snow
Ned Stark
Gregor Clegane
Cersei Lannister
Tyrion Lannister
Sandor Clegane
Jaime Lannister
Catelyn Stark
Theon Greyjoy
Arya Stark
Cersei Smith
Ned Jones

Your function should open and read the contents of this input file and figure out which last name(s) occur most frequently in the data, and print the members of that family in ABC order in exactly the format shown below.

If multiple families are tied for the most members, print each of the tied families in the same format. For example, in the data at right, the largest families are Stark and Lannister, each of which has 3 members listed, so your function should print the Lannisters in ABC order and then the Starks in ABC order.

For example, if the input above is in families.txt, then the call of biggestFamily("families.txt"); should print:

Lannister family: Cersei Jaime Tyrion
Stark family: Arya Catelyn Ned

It is possible that more than two families might tie for the most members. One example of such a case is if every person in the file has a different last name. In such a case, you should print all of the tying families in this same format.

If the file is missing or unreadable, your function should throw a string exception. If the file exists, you may assume that it contains at least one name, that every line of input in the file is in the exact valid format described above, and that no two lines of the file will be exactly the same (though a given first or last name might occur multiple times).

Constraints:

  • You may open and read the file only once. Do not re-open it or rewind the stream.
  • You should choose an efficient solution. Choose data structures intelligently and use them properly.
  • You may create up to two collections (stack, queue, set, map, etc.) or nested/compound structure as auxiliary storage. A nested structure, such as a set of vectors, counts as one collection. A temporary collection variable that is merely a replica or reference to some other collection (such as, Stack v = myQueue.dequeue();) is fine and does not count as a second structure. (You can have as many simple variables as you like, such as ints or strings.)
Type your C++ solution code 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

Need help?

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.