CMSC--202 Computer Science II for Majors
Fall 1997 22 November 1997 Homework 8
The purpose of this homework is to give you some experience with the qsort routine available in most C libraries. It will also introduce you to an array-sorting method that involves pointers into a fixed, unsorted array.
The Air Force has been compiling a database of Martian space-travelers in residence at Roswell, New Mexico. It's a simple data-base composed of first name, last name, and MSSN (martian social security number). Unfortunately, searching the database is difficult because it is not sorted in any way. Your task is to provide three sorted lists of Martians, one by first name, one by last name, and one by MSSN. The Air Force has determined that Martian names and SSNs follow these rules:
Sample output from the program is given on page .
The following figure illustrates how an array of pointers can be sorted so its elements point to the data in another array in increasing numerical order. The ``data'' array contains integers in no particular order. The ``pointer'' array contains pointers sorted so they point to the elements of the ``data'' array in increasing order. The first pointer points to the integer with lowest value; the second pointer points to the integer with the next higher value, and so forth. The last pointer points to the integer with highest value.
In this homework, there will be three separate arrays of pointers and the data array will not contain integers, but the idea is the same as shown in the figure. There are two major advantages to sorting pointers rather than the data itself.
Remember that the rint202(int low,int high) function returns a random integer between low and high, inclusive. Since a char is an integer type, rint202 can be used to return a random character in a given range.
Here is a sample of the output generated by the hw8 program. The Martian data base is printed four times: unsorted, sorted by first name, sorted by last name, and sorted by MSSN.
>> hw8 hw8 <number of martians> >> hw8 foo Command line argument must be an integer >> hw8 5 ----- Before Sorting ----- Last Name First Name Mars SSN tirtxQ kfobtevfyV 446634851 geigblzjcvhU cvalficJ 195842815 rqmerxjmgdlgaegkrnE lnxxvfbgiJ 689424177 qviE ihtyV 776036814 fdnhlucmscenK fcezoC 266260041 ----- Sorted by First Name ----- Last Name First Name Mars SSN geigblzjcvhU cvalficJ 195842815 fdnhlucmscenK fcezoC 266260041 qviE ihtyV 776036814 tirtxQ kfobtevfyV 446634851 rqmerxjmgdlgaegkrnE lnxxvfbgiJ 689424177 ----- Sorted by Last Name ----- Last Name First Name Mars SSN fdnhlucmscenK fcezoC 266260041 geigblzjcvhU cvalficJ 195842815 qviE ihtyV 776036814 rqmerxjmgdlgaegkrnE lnxxvfbgiJ 689424177 tirtxQ kfobtevfyV 446634851 ----- Sorted by Mars SSN ----- Last Name First Name Mars SSN geigblzjcvhU cvalficJ 195842815 fdnhlucmscenK fcezoC 266260041 tirtxQ kfobtevfyV 446634851 rqmerxjmgdlgaegkrnE lnxxvfbgiJ 689424177 qviE ihtyV 776036814
Submit your work in the usual way, using the submit command. For example, to submit the file foo.c, do:
submit cs202_01 hw8 foo.c
submit cs202_02 hw8 foo.c
Submit all the files required to successfully compile your homework using your submitted makefile. The only constraints on file names are that the executable must be named hw8 and the makefile must be named Makefile or makefile. Otherwise, you may name your files as you wish.