Lab 8

Lab assignment: Structs for the storage and retrieval of data

This week in lab we'd like you to familiarize yourself with the structure of structs.  We've given you some sample code which uses some struct objects, and we'd like you to implement some algorithms over them.  When you get used to them, it's like defining your own data types, but the syntax can be weird so here's your time to practice and ask questions.  This assignment is similar to writing a frontend for a really basic database.  We're not even worrying about adding and removing elements yet, though if you wanted to practice dynamic allocation in the context of structs go right ahead.  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 using data I produced from some database.  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.

This week's lab will be in C, though it is okay if you use C++ style input and output.  Structs are primarily a C construct, whereas classes with functions appear only in C++.  Your book uses C++ structs, which are as powerful as C++ classes, so the syntax and functionality is different from C structs.

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.

Notes:

Submission:

1. You should work with a partner for this (and all the remaining) lab(s).  Only one of you need to submit the program to Blackboard, though you should be certain that both of your names be present in a comment at the top of the .c or .cpp source file.

1.5 If you work alone, include just your name in the comment at the top so that I know you worked alone, and not have to guess.

2. You should turn in to Blackboard by the END OF THE LAB (8:50 for the 8-9 lab session, 9:50 for the 9-10 lab session).  I know it's tempting to keep working on it, but other classes come in, and it's not fair to the students who are limited to that particular time span if you go over.  Which isn't to say that you can't work on it later, to check your solution against the one I post for your own understanding.  But what you submit for a grade should be before the next hour begins.

3. If you wish, you may submit your lab by 11 am on Thursday for a 1 point penalty.  If you can't finish up the second point by the end of lab, you can still earn the score by completing all three steps and submitting your code by the day after.