Programming+a+SortedCollection

Now you have struct(ure)s called OrderedCollections. (You may use RiksOrderedCollection) It is easy to use one to hold onto a SortedCollection.

A SortedCollection is "just" an OrderedCollection where the elements are kept in an order determined by comparing some aspect of the elements.

The next step is not difficult, but sometimes people can get confused. To keep it straight-forward, let's nail down the example.

We need some concrete (as opposed to abstract) things to sort. Let's use a toy struct(ure) named "Student" code format="c" struct Student { char *name; int SID; char *phone; float height; // cm. int birthday; // {0..366} Yes, people are born on February 29th. int programPoints; int examPoints; int participationPoints; } code Note that Student does not, and should not, include things such as "Current Index in some sorted collection". Conversely, a sorted collection should be/have a well-ordered collection of references to Students. Here's a file of sample data fitting the above struct: [|StudentData.csv]

Sample reader program (which you may use) is

First, make a collection called StudentsBySID. Use the simplest-to-program sorting method you can. KISS. You should create several well-named procedures. Once you have that programmed and tested, make a collection called StudentsByBirthday, which should be a LOT like the other. Even if you already know better, keep it simple and do copy/paste programming to make variations on the theme, copying procedures to new procedures with similar names. When both of those are tested, build a collection called StudentsByHeight. And finally, StudentsByName.

A typical main procedure for this assignment might in clude code a lot like: code format="c" buildStudentsBySID; /* builds a global SortedCollection named StudentsBySID from the global collection named Students. */       buildStudentsByBirthday; buildStudentsByHeight; buildStudentsByName; // Like Joseph's and Laura's, those collections can all co-exist at the same time.

// Having those Sorted Collections, the following lines can done in any order showStudentsByName; showStudentsByBirthday; showStudentsBySID; showStudentsByHeight; showStudentsByName; // if I wanted to see it again, NO MORE sorting would be needed // The several sorted collections can just sit there, waiting to be referenced. // Laura and Joseph can each have their own deck of cards which REFER to the massive men at the front of the room... // Your sorted collections should just refer to the same Students in the collection studs // In C, a collection of pointers will do the job. printf("The median height is %f\n", studentsMedianHeight); printf("The median name is %s\n", studentsMedianName); // Frivolous, but might be fun to try code I have seen a student project which got the job done. They just made anOC of pointers to Student, and inserted new pointers based on comparing the values of the height (or whatever) of the respective Students. Here is their quite sufficient output. Note that when the OC's are displayed, they show addresses (shortend form). The number in the column to the left of names represents the memory address of each student. Note that it does not change for any student from one OC to the next. The guys don't move: the pointers do. code prog3 < StudentData.csv Read '| "Tom" | 11 | "360-123-4567" | 190 | 190 | 123 | 99 | 20 ' Read '| "Hick" | 35 | "Two shorts" | 185 | 27 | 75 | 88 | 15 ' Read '| "Derry" | 145 | "Penn. 65-000" | 160.9 | 355 | 101 | 99 | 5 ' Read '| "Kathy" | 14 | "546-4646" | 150 | 257 | 111 | 88 | 17 ' Read '| "Jhon" | 144 | "546-4646" | 161 | 157 | 111 | 88 | 17 ' Read '| "Phat" | 288 | "546-4646" | 171 | 60 | 111 | 88 | 17 ' Read '| "Ghengis" | 77 | "546-4646" | 191 | 1 | 111 | 88 | 17 ' Read '| "Taro" | 783 | "81-448-4380" | 170 | 186 | 130 | 110 | 2 ' Read '| "Chen" | 256 | "512-1024" | 170 | 190 | 90 | 90 | 1 ' Read '| "Milan" | 4096 | "256-2048" | 183.3 | 1 | 95 | 95 | 11 ' Read '| "Ethelred" | 2 | "None" | 199.99 | 367 | 13 | | 5 ' Read '| "Attila" | 666 | "2" | 190 | 1 | 0 | 0 | 0 '

Students, just as they are read in:
 * Tom | 11 |360-123-4567 | 190.00 | 190 | 123 |  99 |  20
 * Hick | 35 |  Two shorts | 185.00 |  27 |  75 |  88 |  15
 * Derry | 145 |Penn. 65-000 | 160.90 | 355 | 101 | 99 |   5
 * Kathy | 14 |    596-4646 | 150.00 | 257 | 111 |  88 |  17
 * Jhon | 144 |   546-4646 | 161.00 | 157 | 111 |  88 |  17
 * Phat | 288 |   546-4646 | 171.00 |  60 | 111 |  88 |  17
 * Ghengis | 77 |    546-4646 | 191.00 |   1 | 111 |  88 |  17
 * Taro | 783 | 81-448-4380 | 170.00 | 186 | 130 | 110 |  2
 * Chen | 256 |   512-1024 | 170.00 | 190 |  90 |  90 |   1
 * Milan |4096 |   256-2048 | 183.30 |   1 |  95 |  95 |  11
 * Ethelred |  2 |        None | 199.99 | 367 |  13 |   2 |   5
 * Attila | 666 |          2 | 190.00 |   1 |   0 |   0 |   0

Students sorted by SID theData @896 firstIx=3 lastIx=14   size=12  capacity=20 0: 3 @ 908:  720 |Ethelred |   2 |        None | 199.99 | 367 |  13 |   2 |   5 1: 4 @ 912:   80 |     Tom |  11 |360-123-4567 | 190.00 | 190 | 123 |  99 |  20 2: 5 @ 916:  272 |   Kathy |  14 |    546-4646 | 150.00 | 257 | 111 |  88 |  17 3: 6 @ 920:  144 |    Hick |  35 |  Two shorts | 185.00 |  27 |  75 |  88 |  15 4: 7 @ 924:  464 | Ghengis |  77 |    546-4646 | 191.00 |   1 | 111 |  88 |  17 5: 8 @ 928:  336 |    Jhon | 144 |    546-4646 | 161.00 | 157 | 111 |  88 |  17 6: 9 @ 932:  208 |   Derry | 145 |Penn. 65-000 | 160.90 | 375 | 101 | 99 |   5 7: 10 @ 936:  592 |    Chen | 256 |    512-1024 | 170.00 | 190 |  90 |  90 |   1 8: 11 @ 940: 400 |    Phat | 288 |    596-4646 | 171.00 |  60 | 111 |  88 |  17 9: 12 @ 944: 784 |  Attila | 666 |           2 | 190.00 |   1 |   0 |   0 |   0 10: 13 @ 948: 528 |    Taro | 783 | 81-448-4380 | 170.00 | 186 | 130 | 110 |   2 11: 14 @ 952: 656 |   Milan |4096 |    256-2048 | 183.30 |   1 |  95 |  95 |  11

Students sorted by birthday theData @976 firstIx=2 lastIx=13   size=12  capacity=20 0: 2 @ 984:  464 | Ghengis |  77 |    546-4646 | 191.00 |   1 | 111 |  88 |  17 1: 3 @ 988:  656 |   Milan |4096 |    256-2048 | 183.30 |   1 |  95 |  95 |  11 2: 4 @ 992:  784 |  Attila | 666 |           2 | 190.00 |   1 |   0 |   0 |   0 3: 5 @ 996:  144 |    Hick |  35 |  Two shorts | 185.00 |  27 |  75 |  88 |  15 4: 6 @1000:  400 |    Phat | 288 |    546-4646 | 171.00 |  60 | 111 |  88 |  17 5: 7 @1004:  336 |    Jhon | 144 |    546-4646 | 161.00 | 157 | 111 |  88 |  17 6: 8 @1008:  528 |    Taro | 783 | 81-448-4380 | 170.00 | 186 | 130 | 110 |   2 7: 9 @1012:   80 |     Tom |  11 |360-123-4567 | 190.00 | 190 | 123 |  99 |  20 8: 10 @1016: 592 |    Chen | 256 |    512-1024 | 170.00 | 190 |  90 |  90 |   1 9: 11 @1020: 272 |   Kathy |  14 |    546-4946 | 150.00 | 257 | 111 |  88 |  17 10: 12 @1024: 208 |   Derry | 145 |Penn. 65-000 | 160.90 | 355 | 101 | 99 |   5 11: 13 @1028:  720 |Ethelred |   2 |        None | 199.99 | 367 |  13 |   2 |   5

Students sorted by Height theData @1056 firstIx=1 lastIx=12   size=12  capacity=20 0: 1 @1060:  272 |   Kathy |  14 |    546-4646 | 150.00 | 257 | 111 |  88 |  17 1: 2 @1064:  208 |   Derry | 145 |Penn. 65-000 | 160.90 | 355 | 101 | 99 |   5 2:  3 @1068:  336 |    Jhon | 144 |    546-4646 | 161.00 | 157 | 111 |  88 |  17 3: 4 @1072:  528 |    Taro | 783 | 81-448-4380 | 170.00 | 186 | 130 | 110 |   2 4: 5 @1076:  592 |    Chen | 256 |    512-1024 | 170.00 | 190 |  90 |  90 |   1 5: 6 @1080:  400 |    Phat | 288 |    546-4646 | 171.00 |  60 | 111 |  88 |  17 6: 7 @1084:  656 |   Milan |4096 |    256-2048 | 183.30 |   1 |  95 |  95 |  11 7: 8 @1088:  144 |    Hick |  35 |  Two shorts | 185.00 |  27 |  75 |  88 |  15 8: 9 @1092:   80 |     Tom |  11 |360-123-4567 | 190.00 | 190 | 123 |  99 |  20 9: 10 @1096: 784 |  Attila | 666 |           2 | 190.00 |   1 |   0 |   0 |   0 10: 11 @1100: 464 | Ghengis |  77 |    546-4696 | 191.00 |   1 | 111 |  88 |  17 11: 12 @1104: 720 |Ethelred |   2 |        None | 199.99 | 367 |  13 |   2 |   5 The median height is 177.14999999

Students sorted by Name theData @1136 firstIx=0 lastIx=11   size=12  capacity=20 0: 0 @1136:  784 |  Attila | 666 |           2 | 190.00 |   1 |   0 |   0 |   0 1: 1 @1140:  592 |    Chen | 256 |    512-1024 | 170.00 | 190 |  90 |  90 |   1 2: 2 @1144:  208 |   Derry | 145 |Penn. 65-000 | 160.90 | 355 | 101 | 99 |   5 3:  3 @1148:  720 |Ethelred |   2 |        None | 199.99 | 367 |  13 |   2 |   5 4: 4 @1152:  464 | Ghengis |  77 |    546-4696 | 191.00 |   1 | 111 |  88 |  17 5: 5 @1156:  144 |    Hick |  35 |  Two shorts | 185.00 |  27 |  75 |  88 |  15 6: 6 @1160:  336 |    Jhon | 144 |    546-4646 | 161.00 | 157 | 111 |  88 |  17 7: 7 @1164:  272 |   Kathy |  14 |    546-4646 | 150.00 | 257 | 111 |  88 |  17 8: 8 @1168:  656 |   Milan |4096 |    256-2048 | 183.30 |   1 |  95 |  95 |  11 9: 9 @1172:  400 |    Phat | 288 |    546-4646 | 171.00 |  60 | 111 |  88 |  17 10: 10 @1176: 528 |    Taro | 783 | 81-448-4380 | 170.00 | 186 | 130 | 110 |   2 11: 11 @1180:  80 |     Tom |  11 |360-123-4567 | 190.00 | 190 | 123 |  99 |  20

code Please don't be tempted to copy this and submit it as your output.