CSci 152: Computer Programming II

Fall 2004

Programming Assignment #2

The solution given here shows an example of how one might go about writing a C++ program that satisfies all of the requirements of assignment #2 and runs correctly.  In this solution I have used a typedef statement in order to define 2 new types, a ListType and a BucketType, which are 1-d and 2-d arrays respectively.  These 2 types are used as the argument types to pass around the lists that are to be sorted, displayed, etc. 

I have broken the sorting procedure up into 2 functions.  This is an example of how you can keep your code from becoming too complicated by being too deeply nested.  Here I keep things simpler to read by implementing the outer loop of the sort (that checks if we are done, and keeps track of the radix) in the function radixSortList().  An single pass of the inner loop, where we copy things into buckets based on radix, then copy back out to original loop, is then implemented in radixSortPass()

Take note of the programming style of the suggested solution.  Most of the guidelines from the CPP style guidelines document have been followed and you may see examples of them here. 

 

RadixSort.cpp – An example solution for Assignment #2.  We break the sort into 2 functions, one to implement the outer loop, where we keep passing through the array until the sort is done, incrementing the radix appropriately.  Then the other function implements the inner part of the sort, where we copy items into buckets based on radix, then back to original list.  Support functions are included to generate a random list and to display a list.

Programming Assignment #2 example solution

Example Solution