Assignments | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit

Saint Louis University

Computer Science 180
Data Structures

Michael Goldwasser

Spring 2014

Dept. of Math & Computer Science


Lab Assignment 03

Topic: Symmetric Order
Source Code: order.cpp
Live Archive Ref#: 3055

Pre-lab Due:

Tuesday, 4 February 2014, 10:00am
Submission Deadline: Wednesday, 5 February 2014, 11:59pm

Techniques:

Use of an array or stack.

Collaboration Policy

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.


Pre-Lab Requirement

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


Symmetric Order

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

Hints

Logistics

First we will note that for this problem it is difficult to easily discern correctness when input and output are interleaved as you type. For that reason, we strongly suggest people use the technique (described here) for testing your program using input saved in a file. This can be done using a command such as

./order < myInput.txt
if your executable is named order and the input is saved in file myInput.txt.

With that said, 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.

Array for Storage

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.

Stack for Storage

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


Judge's Data

You can run the automated judge's tests on turing to test the correctness of your program (although you must still formally submit the source code via the course website). If you are working on your own machine (or if you just want to examine the judge's inputs and expected outputs), we provide them here.


Michael Goldwasser
CSCI 180, Spring 2014
Last modified: Monday, 03 February 2014
Assignments | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit