ECE 1021

HOMEWORK #5

(Last Modified: 27 Nov 2010 )

PROGRAM A - Railroad Stack

Programming Problem #7.14 (p384).

MODIFICATION: The problem does not specify how the information is to be displayed and some of the descriptions are misleading. 

The problem is decomposed in the file 05A0SOLN.TXT to a pretty deep level. There are twenty functions that are called by the pseudocode that are not decomposed any further. They are listed at the end of the file and your pseudocode submission is to provide the pseudocode for all of these functions. Don't panic - these functions are at such a low level that most can be implemented in three lines of code or less.

Your final program, of course, must implement all of the functions - and must document any deviations from the pseudocode whether the pseudocode in question was provided or is what you submitted. You do not need to document changes to the parameter lists in the pseudocode since those are implementation issues and the pseudocode is ideally implementation independent - the purpose of including the parameter lists with the pseudocode is just to aid in the implementation and it is understood that exactly what parameters must be passed depends on how the data is represented - it could, in theory, all be global so that nothing needs to be passed.

PROGRAM B - Magic Squares

Programming Problem #7.19 (p386).

MODIFICATION: Allow the user to enter the size of the magic square to be generated. You must at least be able to accommodate (odd) magic squares between 3 and 11 (inclusive). Give some thought to your screen presentation and use character graphics to make the display look nice.

This program should not take long to implement - you might want to do it first to be sure that you get all of the points on it since both programs are weighted equally.

Hints on Program 5A

In looking over HW#5 more closely, I see that there is one aspect that, while very doable the right way, is almost certainly going to lead people into doing it the wrong way (i.e., a way that totally violates the concept of a stack's black-box nature). This has to do with how you update the display of the cars that are on the siding.

The natural way for people to approach this is to have some kind of a loop that goes through position by position and draws the contents of the array. That's fine for the main rail - it's not a stack. But the siding is a stack. If you've read the material about stacks that's linked on the first page you'll remember that the idea is that you have functions that manage the stack and those functions are the only ones that can directly access (either to read or write) the data stored on the stack. One way to do it is to have a PrintStack() function that is part of the stack manager and if you want to print the entire contents of the stack you simply call that function. With a stack this small this is a viable approach. But, as with most things, we want to learn how to do things a bit more elegantly so that we can scale our stack up to thousands or millions of elements and still use the same function. 

The problem with a PrintStack() function is that it would not have any choice but to draw something for every storage location in the stack - whether there was something there or not - since it would have no way of knowing how long it had been since it had been called last and what changes to the stack had taken place. Consider what would happen if this wasn't the case and it only printed out its actual contents at that time. You push three cars onto the siding and then called PrintStack(). It prints out the names of the cars at all three positions. Now you pop two cars off the siding and call PrintStack() again. It draws the name of the car at the first position but the names of the other two cars are still there - they don't go away unless you PrintStack() function specifically prints what needs to be shown at empty stack locations.

So, what's this "elegant" approach I alluded to? It's pretty simply, actually. If you are doing a good job at maintaining the integrity of the stack manager, then the only functions that can change what is stored on the stack are the Push(), Pop(), and Flush() functions. Have them update the display of the stack elements to reflect the changes they have made to the stack.