Lab 8
This week in lab we will be dealing with structure of structs. We've given you some sample code which uses some struct objects, and we'd like you to implement sorting algorithms over them. The "functions" you should implement for this lab are:
1. Print out the database
2. Sort the data by a given criterion
3. Find a particular item from the database
Note that you need not actually use methods or functions to implement this functionality.
In the code you're given the declaration for an array of structs, as well as the instantiation of elements of that array. Also, you are given the structure of the code, which prints out the database, calls a sort function on the data, and then searches through the data for a particular value.
A sample run of the program will look like the following. (User input is shown in bold, though in your program it will not be bold.)
For one point:
Data:
Adams, Suma 768454006 37.000000
Alex, Cris 680378908 5.000000
Alvino, Jason 721777193 69.000000
Amrhein, Maria 798094980 1.000000
... // I'm not going to list all 75 lines here.
Vinot, Hill 684654477 73.000000
Warner, Jill 658026216 61.000000
Williams, Michael 785019931 78.000000
For two points:
Which field would you like to sort by?
1. First Name
2. Last Name
3. UIN
4. Grade
3
Sowles, Charles 650993131 83.000000
Villalobos, Patricia 651055147 7.000000
Kinney, Brian 653179869 52.000000
Nash, Christopher 653856107 27.000000
... // Again, I don't think you need all this data in a lab prompt.
Verhoeven, Andrew 791215097 75.000000
Goehring, Brian 794717105 78.000000
Amrhein, Maria 798094980 1.000000
And for extra credit:
if user choice was 2
Which last name would you like to look up? Siebert
At index 64
Siebert, Sabrina 706562581 7.000000
if user choice was 3
Which UIN would you like to look up? 709418024
At index 28
Minielly, Gary 709418024 23.000000
if user choice was 4
Which grade would you like to look up? 57
At index 47
Flagg, Megan 756141672 57.000000
and an example where the object could not be found (happens to be sorted by first name (user choice 1))
Which first name would you like to look up? Sean
No Student was found with the first name Sean.
Use only structs from C, the syntax is different for C++ (largely how you treat it as a type). This means you will have to use typedef notation when defining your struct to make it work with the sample code. You may use any of the library functions for C string operations. Wherever possible, you should use the builtin and library C functions instead of looping through the characters yourself.
Stage 1 (1 point):
Print out all of the struct objects in the format "LastName, FirstName UIN Grade". Use tabs as appropriate to line up the columns. If the spacing doesn't its OK. The data is already given to you in an array of structs, so just set up a loop to access and print out the data. Reserve 20 characters for the char[]s for first name and last name. As a starting point you should use the sample file called Lab8.c provided at the bottom of this page. The file given demonstrates how to instantiate structs, and provides the data in an array of structures, but does not include the struct definitions or their usage, and will not compile without the it.
Stage 2 (1 point):
Sort the array of struct objects by either FirstName, LastName, UIN, or Grade, depending on user input. After sorting the array, print out the array again. You can apply any sorting technique you wish, and keep in mind that you can perform assignment of structs, which will automatically assign each of the fields. In general you should be careful about char[]s though, as they might be pointing to the same location after an assignment, instead of actually reserving the space. This depends on how the struct is constructed, with a pointer or actually with the full memory allocated in the struct. In this case, if you declare a fixed size for the character arrays in your struct, assignment should work correctly with no issues.
Stage 3 (Extra Credit) (1 point):
Ask the user for an element matching the one sorted by, and then perform a binary search of your array to find that structure and print out the index of that element in the newly sorted array, and the line for that structure. If you can't find the object in the array, then print out that the object could not be found.