CS 631 - Computing Fundamentals II - Spring 2005
Programming Assignment 3
Loyola College >
Department of Computer Science >
CS 631 >
Programming Assignments >
Programming Assignment 3
Pancake Problem
Due
Tuesday, March 29th at the beginning of class.
Email an electronic copy by attaching each .cpp and .h to the email. In addition, turn in a printout of the code. Be sure to include the Honor Code Statement:
"I hereby declare that I have abided by the Honor Code during this assignment."
Introduction
Stacks and Queues are often considered the bread and butter of data structures and find use in architecture, parsing, operating systems, and discrete event simulation. Stacks are also important in the theory of formal languages.
This problem involves both butter and sustenance in the form of pancakes rather than bread in addition to a finicky server who flips pancakes according to a unique, but complete set of rules.
Given a stack of pancakes, you are to write a program that indicates how the stack can be sorted so that the largest pancake is on the bottom and the smallest pancake is on the top. The size of a pancake is given by the pancake's diameter. All pancakes in a stack have different diameters.
Sorting a stack is done by a sequence of pancake ``flips''. A flip consists of inserting a spatula between two pancakes in a stack and flipping (reversing) all the pancakes on the spatula (reversing the sub-stack). A flip is specified by giving the position of the pancake on the bottom of the sub-stack to be flipped (relative to the whole stack). The pancake on the top of the whole stack has position 1 and the pancake on the bottom of a stack of n pancakes has position n.
A stack is specified by giving the diameter of each pancake in the stack in the order in which the pancakes appear.
For example, consider the three stacks of pancakes below (in which pancake 8 is the top-most pancake of the left stack):
| 8 | 7 | 2 |
| 4 | 6 | 5 |
| 6 | 4 | 8 |
| 7 | 8 | 4 |
| 5 | 5 | 6 |
| 2 | 2 | 7 |
The stack on the left can be transformed to the stack in the middle via flip(4) where 4 is the number of cakes flipped. The middle stack can be transformed into the right stack via the command flip(6).
The Input
The input consists of a sequence of stacks of pancakes written in a file. Each stack is given as a single line of input with the top pancake on a stack appearing first on a line, the bottom pancake appearing last, and all pancakes separated by a space. The diameters of all pancakes will be whole numbers. The file will be terminated by an end-of-file marker.
The Output
For each stack of pancakes, echo the original stack on one line, followed by some sequence of flips that results in the stack of pancakes being sorted so that the largest diameter pancake is on the bottom and the smallest on top. For each stack the sequence of flips should be terminated by a 0 (indicating no more flips necessary). Once a stack is sorted, no more flips should be made.
Sample Input
1 2 3 4 5
5 4 3 2 1
5 1 2 3 4
Sample Output
1 2 3 4 5
0
5 4 3 2 1
5 0
5 1 2 3 4
5 4 0
Assignment
In order to complete this assignment, you may use two data structures, an Iterative Stack and a Queue. Unlike the stacks we studied in Chapter 6, an iterative stack allows you to read the values stored in the stack in order from top to bottom. We will study queues in chapter 7. The queue is a data structure that follows the rule first in, first out or FIFO, unlike a stack which follows the rule last in, first out or LIFO. A queue mimicks the behaviour of a line at the bank. The first customer to arrive will be the first one served. For this assignment, the Iterative Stack will be the primary data structure, although you will need secondary storage when you are flipping a stack. You should use the Standard Template Library's queue.
- You will modify the pointer implementation of the Stack on pp. 296-299 of the text to create an Iterative Stack ADT (StackException.h, StackP.h, and StackP.cpp). The Itertative Stack includes an iterator so that you have access to all items in a stack. For this problem, you need an iterator that outputs the items from top to bottom. Add the following methods to Stack:
- begin which takes no parameters and returns nothing. The method should set the iteterator to the top of the stack.
- hasNext which takes no parameters and returns a Boolean value. The method should determine if there are more objects to read in the stack.
- getNext which takes a parameter of a reference type to StackItemType and returns nothing. This method should set the item parameter to the next item in the stack. Throw a StackException when appropriate.
- Hint: You may need to add a private instance variable to the Stack
- You will also create a class PancakeStack, which has as a minimum the following methods:
- constructor which takes as a parameter a string representing an unsorted stack of pancakes and creates a PancakeStack.
- flip which takes as a parameter the number of pancakes to flip and returns nothing. This method should flip that number of pancakes.
- whereToFlip which takes no parameters and returns some number of pancakes to flip.
- toString which takes no parameters and returns a string which is the current state of the stack of pancakes printed from top to bottom.
The class and functions may only use stacks and queues for storing data. Include the <queue> header file. This class is described in Appendix E in the text book. There is an example of using the queue class here.
- Your program should first call a function announce to describe the purpose of your program.
- You should ask the user for a file name that stores the pancake data
- Produce the output described above.
- Design your program using a modular approach. You are not allowed to use global variables, although you may define global constants.
Suggestions
Begin by designing the flip and whereToFlip methods and making the changes to Stack. Then create the other methods that support the PancakeStack and write the main function. Test throughly to make sure that your program handles all kinds of pancake stacks.
Grading
- 70% Execution - Partial credit will be granted depending on how much progress you made towards the correct result.
- 20% Design - The design score is based on how easy it is to follow the logic of your code, how well you avoided repetitive code, the types you choose for your variables, and how easy it would be to change your code if certain specifications changed.
- 10% Style - Style includes comments, indentation, and choice of variable and method names.
- Substantial progress must be made towards correct execution to earn the points for design and style.