(Latest Revision: 09/25/99)

Making Versions of a List Program

CS 2500 PROGRAMMING ASSIGNMENT THREE
Part One: Making and Sorting a List of Structures


DUE DATE:

Please read pages A7, A11-A15, and A30-A50 in Appendix A of your textbook (Carrano) before you read this document.

Also, you are expected to read the class documents entitled:
before beginning to do this or any programming assignment. You will find the documents under "CourseDocuments" in the class web space.

SYNOPSIS

For this part of the assignment, you will write a program that reads some data, sorts it, and then writes out the sorted data using a special format.

INPUT

The data that your program will read consists of a series of records. Records are separated by a blank line. Each record has four fields, one field per line. The records are simplified inventory records, like the inventory records a grocery store might keep. Here is one example record:

20115
laundry-soap
87
3.60

The four fields, in the order given above, are the item serial number, item name, quantity in stock, and the item price.

Your program will read a series of such records from a file named "inventory". A sample inventory file is included in this directory. Look it over. Your program must be able to read *any* file of this form, as long as the number of records does not exceed 100. Note that the last line of the file contains the number 00000. This is a sentinel that your program may use to determine that there are no more records to read.

DATA TYPES (The representation of data)

Your program must use the data types declared in the header file named structSort.h that is contained in this directory. Read that file now (it is very short) before continuing. Make a copy of structSort.h and use a

#include "structSort.h"

directive to include it in your program. You will not need to include any other files because structSort.h includes everything you need.

The file structSort.h defines two data types with typedef statements. The data types are inventoryType and inventoryItemType.

Both of these data types are examples of C structs. Structs are data types that are designed to hold a heterogenous set (a mix) of fields.

A variable of type inventoryItemType is designed to be a simplified model of one kind of item that a grocery store might have in its inventory, like gallon-of-milk, or loaf-of-bread. Each variable of type inventoryItemType has space for storing four fields: the serial number for a particular kind of item, its name, the quantity of the item on hand, and the price per item.

A variable of type inventoryType is designed to be a simplified model of an entire inventory. An inventory is a list of all the kinds of items that a particular store offers for sale. A variable of type inventoryType contains an array whose individual components are of type inventoryItemType. The array contains 100 slots, so the array has room to represent up to 100 different kinds of inventory items. A variable of type inventoryType also contains an integer field called numItems. The programmer can use numItems to keep track of which array components are actually being used to store information about inventory items. For example, if numItems equals 12, it means that slots 0 through 11 in the array are in use.

PROCESSING

In your main program, declare a variable of type inventoryType. Let's suppose you name that variable "inv".

Your program must read the inventory file, and copy the data in the records there into the array inside inv (the name of the array field is inventoryItem). Your program must copy the data from the first item in the file into inv.inventoryItem[0], the data from the second into inv.inventoryItem[1], and so on, until all the data has been copied from the file. Also, your program must make sure that inv.numItems is set equal to the number of items that are actually in use in the array. For example, if items 0 through 9 are in use in the array, then inv.numItems should equal 10.

After it gets the data from the file into inv, your program must sort inv.inventoryItem by increasing serial number. I have given you a set of functions that will do that job. Look in the file named sortCode in this directory. Insert the contents of that file into your program in the proper place, and put a call to the function SortStructs in the proper location in your main program. SortStructs depends on you to correctly copy the data into inv.inventoryItem, and to correctly set the value of inv.numItems before you make your call to SortStructs. Remember "garbage in, garbage out" so be sure to meet these preconditions of SortStructs.

OUTPUT

After the data has been sorted, your program must write the contents of the sorted list to the screen in order of increasing serial number. This must be done in a certain format. Look at the file named "output" in this directory to see a sample of this format. The file called "output" is exactly the output that should come out if the input is the file called "inventory".

DUE DATE: