array element | name | ID |
[0] | HAMEL, GLYNIS | 39602 |
[1] | WARD, MATT | 4412 |
[2] | HEINEMAN, GEORGE | 6074 |
[3] | WILLS, CRAIG | 5570 |
[4] | FISLER, KATHI | 3964 |
We would construct this array to provide access to the data in alphabetical order:
array element | subscript |
[0] | 4 |
[1] | 0 |
[2] | 2 |
[3] | 1 |
[4] | 3 |
The meaning is this: you will find the name closest to the beginning of the alphabet (FISLER, KATHI) at subscript 4 in the original array. The next one in order is at subscript 0 (HAMEL, GLYNIS), and so on. Similarly, to order the original data by increasing ID number, we would construct an array consisting of these values:
array element | subscript |
[0] | 4 |
[1] | 1 |
[2] | 3 |
[3] | 2 |
[4] | 0 |
By using access tables in this way, we can avoid keeping multiple copies of the original data, which would be wasteful of storage space. And if a change needs to be made to one of the student records, we only have to make the change in one place; we're spared the problem of making sure that multiple copies of the data are updated in exactly the same way.
#define MAXSTUDENTS 20 #define MAXSTRING 30 char students[MAXSTUDENTS][MAXSTRING]; // the original list of names char *alphabetic[MAXSTUDENTS]; // array of pointers to the namesStudent names are entered into the students array. After all the names are entered, a sorting routine will be called, and both the original list and the alphabetized list will be displayed. The original list should not be modified by the sorting function. Here is a sample execution:
How many students are enrolled? 5 Enter the names of the students on separate lines: JENKINS, JOHN WANG, JIN SULLIVAN, DANIEL FISHER, KATHERINE BELOIT, KIMBERLY Original Order Alphabetical Order ------------------ ------------------- JENKINS, JOHN BELOIT, KIMBERLY WANG, JIN FISHER, KATHERINE SULLIVAN, DANIEL JENKINS, JOHN FISHER, KATHERINE SULLIVAN, DANIEL BELOIT, KIMBERLY WANG, JIN
alphabetic
array to contain the addresses
of each string in the students
array. Then call a sorting
function to sort the pointers in the alphabetic
array corresponding
to the alphabetical ordering of the strings in the students
array.
strcmp
to compare strings alphabetically). The array you will be passing to the
sorting function will be the alphabetic
array (not the
students
array). Your sorting function should be in a separate
.c file.
strcmp
function, the string "Alcott" comes before the
string "adams" (because the ASCII code for 'A' is smaller than the ASCII
code for 'a'). To keep you from having to deal with this problem, you may enter the students' names in
all capital letters (or in all lower-case letters). (Of course, if you would
like to write your solution so that it does alphabetize mixed upper/lower case
strings correctly, you are welcome to do so.)
alphabetic
array - 5 points
Programs submitted after 5pm on December 11 will be tagged as late, and will be subject to the late homework policy.