Saint Louis University |
Computer Science 180
|
Dept. of Math & Computer Science |
Topic: | Symmetric Order |
Source Code: | order.cpp |
Live Archive Ref#: | 3055 |
Pre-lab Due: |
Thursday, 9 February 2012, 10:00am |
Submission Deadline: | Friday, 10 February 2012, 11:59pm |
Techniques: |
Use of an array or stack. |
The pre-lab requirement must be completed and submitted individually.
The remainder of the lab activity should be completed working in pairs. One person should submit the result, making sure that both partners' names are clearly identified in that submission.
Please make sure you adhere to the policies on academic integrity in this regard.
Read the complete problem description and then determine what the expected output should be if given the following input:
Prelab input: | Prelab output: |
4 Bob Bill Betty Bobby 5 Ed Eddie Eliza Edward Elizabeth 0 |
In your job at Albatross Circus Management (yes, it's run by a bunch of clowns), you have just finished writing a program whose output is a list of names in nondescending order by length (so that each name is at least as long as the one preceding it). However, your boss does not like the way the output looks, and instead wants the output to appear more symmetric, with the shorter strings at the top and bottom and the longer strings in the middle. His rule is that each pair of names belongs on opposite ends of the list, and the first name in the pair is always in the top part of the list. In the first example set below, Bo and Pat are the first pair, Jean and Kevin the second pair, etc.
Input: The input consists of one or more sets of strings, followed by a final line containing only the value 0. Each set starts with a line containing an integer, n, which is the number of strings in the set, followed by n strings, one per line, sorted in nondescending order by length. None of the strings contain spaces. There is at least one and no more than 15 strings per set. Each string is at most 25 characters long.
Output: For each input set print "SET n" on a line, where n starts at 1, followed by the output set as shown in the sample output.
Example input: | Example output: |
7 Bo Pat Jean Kevin Claude William Marybeth 6 Jim Ben Zoe Joey Frederick Annabelle 5 John Bill Fran Stan Cece 0 |
SET 1 Bo Jean Claude Marybeth William Kevin Pat SET 2 Jim Zoe Frederick Annabelle Joey Ben SET 3 John Fran Cece Stan Bill |
The reordering of names can be solved in many ways, but it requires some use of a container to store some names while others are being printed. Particular care is needed to write code that cleanly handles both odd-length and even-length data sets.
Given that they promise that there will be at most 15 strings per set, a natural approach for storage is to declare an array of fixed capacity 15 (you can just ignore some of it if a data set has fewer names). The question will be whether to store them in the given order, and then use logic to produce the output in desired order, or whether to attempt to store them in the output order as you fill up the array, in which case the later loop to print the output is straightforward.
As an alternate approach, for those interested, there is a simple approach to solving this problem based on use of a stack. As you read names in a data set, print the first name, push the second onto the stack, print the third name, push the fourth onto the stack, and so on. Once you have reached the end of the data set, empty the stack one element at a time, printing each item and then popping it.
Its easy to see why this works by tracing through examples. The first pass of reading will result in outputting the 1st, 3rd, 5th names, and so on. For example, in the first sample input, we will have already printed Bo, Jean, Claude, and Marybeth. The other names (e.g., Pat, Kevin, William) are stored on the stack. However, since a stack provides last-in-first-out semantics, the order in which we output these elements is the opposite of the original input order. That is, William will be printed, then Kevin, then Pat.
To implement this approach, you may rely on the C++ STL stack class, included in the library <stack>. You can find online documentation regarding its usage. Please keep in mind that this is a templated class. For this lab, you will be storing strings, so you will need to declare your stack using a syntax such as
stack<string> names; // an initially empty stack