Topic: | Symmetric Order |
Source Code: | order.cpp |
Live Archive Ref#: | 3055 |
In-Class Day: |
Thursday, Sept. 29, 2011 |
Submission Deadline: | Sunday, October 2, 2011, 11:59pm |
Techniques: |
Use of a stack. |
Please review the general information about lab assignments.
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.
For this lab, we want you to implement the following simple approach using a stack. For each set as you read the names in the input, print the first name, push the second onto the stack, print the third name, push the fourth onto the stack, and so on (making sure to properly handle both even and odd-length lists). 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.
Please use 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
4 Bob Bill Betty Bobby 5 Ed Eddie Eliza Edward Elizabeth 0 |