HOMEWORK ASSIGNMENT #3

                                  CSE 1320-005

                                  Spring 2006

 

ASSIGNED: Thursday, March 9, 2006

DUE     : Thursday, April 6, 2006

CONCEPTS: Ch.  7 -- structures and unions

          Ch.  9 -- command line arguments

          Ch. 10 -- file i/o

          Ch. 11 -- linked lists

POINTS  : 100

WEIGHT  : 13%

 

INPUT FILES: 1) lab3_1.dat: CSE1320_LAB3Hires_206dat.dat

             2) lab3_2.dat: CSE1320_LAB3Modifications_206dat.dat

             3) lab3_3.dat: CSE1320_LAB3Transfers_206dat.dat

 

OUTPUT FILE: LAB3_NAME_OUTPUT.dat

 

SOURCE CODE FILES: 1) a constants file

                   2) a data structures definitions file

                   3) a driver file

 

ORIGINAL: 02.20.06 RS  Mod: 03.09.06 jcmt

 

Overview of the Problem:

 

 The Roberts Music Corporation is considering expanding its operations by

opening a new store in the Dallas/Ft. Worth area.  As the Corporation begins

planning of the expansion, it assigns their software group the responsibility

of developing and conducting a pilot study of the new store's operations.

 

 As a member of the software group, you have been given the responsibility

of designing and implementing a new software program to be used to model the

hiring and transferring of employees.  The software you write will be used as an initial study for the larger full-fledged system that would need to be written.

 

 You program will implement the following tasks:

Read the needed employee data file names from the program command line and open these files;

“Hire” new employees by reading employee data from the input files, storing each employee into a struct and creating a sorted data storage structure to hold the employee structs;

Print all employee data;

Read in data to modify employee information and make the appropriate changes;

“Transfer” employees out of this store by removing them from the data storage structure; and

Sort the data storage structure based on different criteria.

 

  The legal requirements imposed upon the personnel department of the

Corporation dictate that the following information be recorded for each

employee:

 

 Last name

 First name  (One word with no spaces)

 Middle initial  (Some employees will NOT have this field)

 ID number

 Job title   (owner, secretary, cashier, custodian, clerk)

 Monthly salary  (may be integer depending on the job title)

 

 

Implementation specifications for the problem:

 

 Since your program is to be part of an initial study, many design decisions must be made before anything can be implemented.  Design decisions include the programming language to use for the study, the data structures to use, and the algorithms to use.  Additionally there are specifications given by the customer and/or user that may also affect the program design.  Following are some of the decisions that have been made and why these decisions have been made for this project.

 The following design decisions have been made:

 

Language:  Because this is an initial or "pilot" study, the choice of languages will be C, to shorten the development time.

 

Data structure for a single employee: Choices include

 

       (a) an array of primitive data types

 

            Pros -- good if each element of employee data is the same type of

                          scalar

            Cons -- not applicable if an element is an aggregate or types are

                         different

 

       (b) a structure  (struct)

 

            Pros -- good for storing multiple fields of different types if

                         all fields need to exist at the same time

            Cons – fields must be accessed uniquely as members not with indexes

 

        (c) a union

 

            Pros -- good for storing multiple fields of different types, but

                         only if one field is needed at a time

            Cons -- cannot support simultaneous access of multiple data

                          fields

 

The most appropriate structure for a single employee is a struct since it will hold all the different types of fields needed for the employee data.

 

Data storage structure: The appropriate data structure(s) need to be chosen early.  The possible options include

 

       (a) an array of primitive data types

 

            Pros -- good if each element is a scalar

            Cons -- not applicable if each element is an aggregate

 

       (b) a structure  (struct)

 

            Pros -- good for storing multiple fields of different types if

                         all fields need to exist at the same time

            Cons -- requires that all objects have the same arrangement of

                          fields

        (c) a union

 

            Pros -- good for storing multiple fields of different types, but

                         only if one field is needed at a time

            Cons -- cannot support simultaneous access of multiple data

                          fields

 

        (d) an x-dimensional array of structs (benefits of structs +)

 

            Pros -- good if the maximum number of elements is known in advance

            Cons -- bad if the array is sparse

 

        (e) a linked list of structs (benefits of structs +)

 

            Pros -- good if the maximum size is not known in advance

            Cons -- one of the most complicated data structures available in C

 

The most appropriate option turns out to be (e), a linked list of structures (structs), because we can simply add a link field to the employee struct and because we do not have to know in advance how many employees there might be.  Therefore, this pilot study will consist of designing and developing a C program using a linked list of structures to model the hiring and transferring of employees into and out of the store.

 

Input and output for the program: Since we will typically be working with groups of employees in our program, it is most useful to input the data through files rather than through direct user input.  Also, as the output during development will probably be more than can be seen at a given time in an editing window, all output of the program will go to an output file with the possible exception for statements executed as a result of testing the success/failure of fopen(), etc., which could send their output to the screen.

 

  In order to give the most testing flexibility to the user of the program the names of all input and output files will be entered and processed as command line arguments.  There are three input files and one output file needed for the program.

 

  The data input of the program will be divided into three input files to divide the problem into manageable blocks of information.  The first file will have all the employee data for the initial employees, the second input file will have data related to salary modifications for employees, and the third input file will be a list of employee IDs for those employees being transferred to a new store after training in our store.  The specific formats for each of these three input files is given below.

 

Employee data file of new “hires”

An example 1st input file "lab3_1.dat" would contain an employee's last name, an employee's first name, an employee's middle initial if any, an employee's id number, an employee's job title, and an employee's monthly salary on a single line.  Each line would represent a different employee.  The data file might look something like:

 

Heineman, Doug A.  264592791  clerk 473

Taffe, Henry A.   374926490   owner 2392.37

Davis, Don R.     194394500  cashier 394

Taffe, Henry C.   28495039  custodian 239.93

Johnson, Mary S.    204395702    clerk  228

Abner, Lil         9403848   secretary 304

Nut, Cashew W. 392749309 clerk 230

 

The monthly salary field represents a monthly salary and will be either of type "int" or of type "double", depending on the value in the title field of the employee.  The possible values of the title field and the corresponding salary field for a given employee are: integer format for secretary, cashier, and clerk, and floating-point format for owner and custodian.

 

The struct for an employee should contain a member field for each piece of data listed in the input file and another data field which will be a pointer to the its own type of structure to use as the link to the next node in the singly-linked list.  There will be a total of 8 data fields in the structure definition.

 

HINT: For the monthly salary, define a union representing the pay type (double or integer) and add a data field of that type to the structure, to aid in determining the type of the monthly salary field of that employee during the execution of the program.

 

Employee data file of employee salary adjustments

An example second input file "lab3_2.dat" would be a list of adjustments to monthly salaries.  This file contains an employee id followed by one or more blank characters followed by an amount.  This salary adjustment amount is either a raise or reduction of the monthly salary of that employee (signified by a + or - preceding the amount of the adjustment respectively).  Example:

 

374926490  +48.93

264592791    -3

463947204   +9

374926490     -49.08

 

Amounts shown for employees with an integer type of salary are listed as integer values in this input file; amounts shown for employees with a floating-point type of salary are listed as floating-point values in this input file

 

Employee data file of employees being “transferred” out of this store

The third input file, "lab3_3.dat", contains IDs of employees who are to be transferred to another store. 

 

 An example "lab3_3.dat" input file might look like

 

264592791

374926491

374926490

 

NOTE: None of the input files are formatted--a varying number of blank-space characters may exist between items on a given row.

 

Output file

The output file should receive all of the printed output produced by any of the tasks described below.  Formats for the output are described with each task.

 

 In addition to the design decisions listed above, the program must perform a set of specific tasks given by the user.  These tasks include processing tasks, error checking tasks, input tasks, and output tasks.  All tasks to be performed are described below.

 

  The basic tasks of this program include the following, each of which might be assigned to its own function, passing parameters by value or reference as necessary :

 

  (1)  test the number of command line arguments

  (2)  open a file using the appropriate command line argument

  (3)  test for successful opening of a file

  (4)  read in a line of data representing an employee

  (5)  divide the line of data in (4) above into its individual tokens and

        store into local variables

  (6)  store the values of the local variables in (5) above into a new object

        of the structure

  (7)  add the object of (6) above into the linked list as a new node

  (8)  print the contents of the linked list to the output file

  (9)  read in a line of data representing an employee's salary adjustment

  (10) update a data field in a node

  (11) remove an employee object from the linked list

  (12) sort the linked list by some criterion

Task (1) in the list is to perform error checking on the command line parameters to make sure that there are the correct number of parameters (5 - program invocation a.out, input file 1, input file 2, input file 3, output file.)  If the command line parameters are correct, print a statement to that effect and continue execution of the program.  Otherwise, print a statement prompting the user as to what should be typed at the command line to run the program and terminate the program.

 

Task (2) is to connect the file variables (FILE *) in the program to the correct  command line parameter file names and open each file for reading or writing as specified.  Choose from the following options for declaring the FILE * variables:  An array of 4 FILE *variables or 4 separate FILE * variables, each with its own unique name

 

Task (3) is to perform error checking on the file variables to insure that each given file is properly opened.  For each FILE * variable, if the file opens correctly, print a statement to that effect and continue execution of the program.  Otherwise print a statement that this input file could not be opened properly (include the name of the input file in the statement) and terminate the program

 

Task (4) is to read in any entire line from the input employee data file of new “hires” (the example file named "lab3_1.dat") as one string of data.

 

Tasks (5) and (6) are to break the “new hire” string into tokens, storing each token substring separately, and then using the tokens to assign the input values into an employee struct.  Make sure to convert strings into numeric data types as appropriate.

 

Task (7) is to add the employee struct from the previous step into the linked list of employees sorted in order by alphabetical order based on the employee's last name.  The program must create the linked list by accepting the input from the first input file (referred to as "lab3_1.dat").  The linked list must be built and maintained in increasing alphabetical order based on the employee's last name, even though the contents of the first input file are not listed in that manner. If two employees in the file have the same last name, they must be added to the linked list in ascending alphabetical order based on the first name.  If two employees have the same last and first name, they must be listed in descending numerical order based on the employee id.

 

The linked list will be represented in main by one pointer variable that will be used to point to the first node in the linked list.  Additional pointers of the same structure type may also be helpful for insertion into the list, etc.

 

As error checking for Task (7), each time a node is added to the linked list (i.e. when each new employee is “hired”), the contents of the updated linked list are to be printed to the output file, as a table in formatted order, one node (struct) per line, including the following data items on each line:

 

     a.  the node's location in the list (1, 2, 3, etc.), beginning with the

          first node as 1.  This "location" CANNOT be a field within the node.

     b.  the address of the node in the linked list, in UPPER-CASE HEXADECIMAL

     c.  the last name of the employee

     d.  the first name of the employee

     e.  the middle initial of the employee (or blank if no initial)

     f.  the employee's id

     g.  the current monthly salary of the employee, and

     h.  the address of the next node in the linked list, in UPPER-CASE

          HEXADECIMAL (NOTE: an address of NULL must be displayed as the

          string "NULL", not 0)

 

 As each line is read in and processed, the output file might begin to look

something like

 

New employee: Heineman, Doug A.  264592791  clerk 473

The updated linked list of employees is:

 

 NUM  CURRENT  LAST        FIRST   MI     ID     MO. SALARY TITLE       NEXT

------------------------------------------------------------------------------

  1  1F3928C0 Heineman     Doug     A 264592791 $     473   clerk         NULL

 

 

New employee: Taffe, Henry A.   374926490   owner 2392.37

The updated linked list of employees is:

 

 NUM  CURRENT  LAST        FIRST   MI     ID     MO. SALARY TITLE       NEXT

------------------------------------------------------------------------------

  1  1F3928C0 Heineman     Doug     A 264592791 $     473   clerk     1F3928D8

  2  1F3928D8 Taffe        Henry    A 374926490 $ 2392.37   owner         NULL

 

 

New employee: Davis, Don R.     194394500  cashier 394

The updated linked list of employees is:

 

 NUM  CURRENT  LAST        FIRST   MI     ID     MO. SALARY TITLE       NEXT

------------------------------------------------------------------------------

  1 1F403940  Davis        Don      R 194394500 $     394   cashier   1F3928C0

  2 1F3928C0  Heineman     Doug     A 264592791 $     473   clerk     1F3928D8

  3 1F3928D8  Taffe        Henry    A 374926490 $ 2392.37   owner         NULL

 

 

New employee: Taffe, Henry C.   28495039  custodian 239.93

The updated linked list of employees is:

 

 NUM  CURRENT  LAST        FIRST   MI     ID     MO. SALARY TITLE       NEXT

------------------------------------------------------------------------------

  1 1F403940  Davis        Don      R 194394500 $     394   cashier   1F3928C0

  2 1F3928C0  Heineman     Doug     A 264592791 $     473   clerk     1F3928D8

  3 1F3928D8  Taffe        Henry    A 374926490 $ 2392.37   owner     1F40394C

  4 1F40394C  Taffe        Henry    C  28495039 $  239.93   custodian     NULL

 

etc.

 

Be sure to left justify the strings and right justify the numbers (fields NUM, CURRENT, ID, MO. SALARY, and NEXT).  Print out integer values in integer notation, and floating-point values in floating-point notation

 

When printing the updated linked lists after each employee is “hired” at the store (as the lines of input are read in and processed) be sure to separate each printout of the linked list by at least two blank lines, as shown in the above example output.

 

Task (8) is to print the contents of the linked list to the output file.  This will be similar to the last table created in Task (7) but does not need the CURRENT and NEXT table entries.  Make sure to label this output clearly.

 

Task (9) is to read in a line of data representing an employee's salary adjustment from the 2nd input file of salary adjustments, example file "lab3_2.dat".  Each line of data in this file has two pieces of information and they can be read directly into appropriate data types.

 

Task (10) uses the data from Task (9) to update the salary field in the appropriate employee’s struct.  Thus Task (10) must search the linked list, find the correct employee and then update the salary with the adjustment from the file.  Be sure to use the correct data types for the salaries.  As each line of input from "lab3_2.dat" is processed, when a salary is updated, print the entire contents of the newly updated linked list.  It is also possible that an employee ID in "lab3_2.dat" does not exist in the linked list (the employee does not work at this store.)  In that case, print a statement to that effect including the value of the ID followed by 2 blank lines, but do not print the linked list, because the linked list was not updated for this line of input.

 

Task (11) will remove an employee object from the linked list based on the ID numbers of “transferred” employees listed in the third input file "lab3_3.dat".  Process each line of data from the third input file "lab3_3.dat" which contains IDs of employees who are to be transferred to another store.  For each ID in the file, if an employee with that ID is still working at this store (in the linked list of employees), delete that node from the list (transfer the employee) and the print the entire updated list.  If no employee with that ID is working at the store, print a statement to that effect but do not print the contents of the linked list, as it was not updated for this line of input.

 

Task (12) will sort the list three different ways printing the list at the end of each sort.  The three sorts are

 

1. Decreasing order by salary (treat integers as floats for this sort).

2. Increasing lexicographical order by title.

3. Increasing numerical order by employee ID.

 

After each of these sorts, print the entire list using the print function from Task (8).  This will demonstrate the correctness of each sort.

 

NOTE:  the actual input files will be posted on the class website.  In the meantime, set up the above example input files and use them to test the code as it is being developed.

 

 

HINT:

 

 Begin this assignment by doing the following--

 

  -- store all of the Lab 2 files in their own subdirectory, including the

 

       -- constants file

       -- driver file

       -- script session

 

  -- make a new subdirectory for Lab 3

  -- copy the constants file from Lab 2 into the new subdirectory

  -- create a new data structures source code file which contains the

      definitions of the union and of the structure.  This file should

      #include the constants file.

  -- create a new driver file for Lab 3, in the new subdirectory.  This file

      should #include the data structures file.

  -- modify both file headers so that they apply to Lab 3

  -- modify the conditional compilation directives so they apply to Lab 3

 

At this point in the assignment, choose the following:

 

  -- function names (based on their purpose--read, write, create, store, etc.)

  -- formal arguments (for both function prototypes and function definitions)

  -- local variable names for the various functions

 

and then:

 

  -- define the union in the data structures file

  -- define the structure below the union definition in the data structures file

  -- compile the data structures file and correct any syntax errors

  -- #include the required system files, in the constants file

  -- declare and initialize as necessary any constants to be used in this

      assignment, in the constants file

  -- prototype each function in the constants file

  -- compile the constants file and correct any syntax errors

  -- stub each function in the driver file, after main()

  -- compile the driver file and correct any syntax errors

  -- expand main() to initialize and test the file handlers

  -- recompile the driver file

  -- expand main() to include a call to the print function

  -- recompile and correct any errors

  -- etc., defining the next function, recompiling, correcting, etc. until

      the program is complete

 

 

Grading:

 

Deductions:

 

If the lab contains any global variables, whether they are used or not (constant variables or symbolic constants are required instead), the lab assignment will receive a zero (0).  Note that structure and other data TYPES should be defined globally.

If the program does not use a singly-linked list of objects of the structure defined in the constants file as the principal data structure and the other required data structures (a union representing the possible pay types of an employee and a structure representing an employee), the lab assignment will receive a zero (0).

A program which produces warnings or errors during compilation, linking or execution will result in an overall grade of 0 (zero)

Use of the goto  command will result in an overall grade of 0 (zero)

Use of the exit command will result in an overall grade of 0 (zero)

If the paper submittal is not a script session using the script command on OMEGA, the lab assignment will receive a zero (0).

If the program was not compiled using the students' own OMEGA account, and/or was not compiled, linked, and executed using the gcc compiler on OMEGA, the lab assignment will receive a zero (0).

Submissions lacking either source code in soft copy or  script files in hard copy  from omega will result in a deduction of points.

Late submission of softcopy to appropriate TA will result in an overall grade of 0 (zero) without prior instructor approval

Use of C language elements not yet discussed in class by the lab due date will result in potential deduction of points – discuss with instructor before using.

 

 

Point values:

 

Style and Documentation (12 points total)

Use of meaningful identifiers for variables, constants, function names, etc.

No hard-coded numbers used except as constants defined in the constants file

Consistent use of braces placement around blocks of code

No more than one instruction per line of code

Consistent and reasonable use of indentation

No wrap-around lines of code 

Line comments are separated horizontally from code being described

Use of whitespace to separate declarations from instructions in function

Use of horizontal whitespace to separate operators from operands

Function header for entire program contains all required information

File header in each file and function headers for all functions

Comments/documentation included in code

 

Functionality and Modularity  (71 points total) (files and functions perform specific tasks as defined)

Declare all the local variable (1)

Test the command line arguments to main (1)

Using the command line parameters, open all the files (4)

Test each file as it is opened (4)

Call all required functions in the order specified above passing the appropriate parameters (5)

Use loops to read all data from files, stopping at file EOF (6)

Read data from the first input file (3)

Create a node with the input data (3)

Add new nodes to linked list in correct sorted order as described (10)

Print entire list as table (4)

Read IDs and salary adjustment data from the second input file (3)

Correctly search linked list for IDs (4)

Print message if ID not in list (2)

Modify employee salary data if ID is in linked list (3)

Sort list by ID (6)

Sort list by salary (4)

Sort list by title (4)

Remove “transferred” employees from linked list (5)

 

Output:     (17 pts total)

User clearly understands what is being requested for input (specific and complete) (1 pt)

Output is accurate (4)

Output gives clear information to explain the values to the user (readability) (2 points)

Output is well-formatted, readable and understandable (3)

Files  (2 points total)

No control characters at the end of each line of code in the script session, causing double spacing

Files conditionally compiled

Script file contents submission requirements   (5 points total)

a 'cat' of File #1  (constants file)

a 'cat' of File #2  (type declarations file)

a 'cat' of File #3  (main file)

a compilation of File #3, using 'gcc'

required directory listings

the run of the program with three sets of data 

 

 

 

TURNIN a paper copy of the script session in class on the due date.

 

EMAIL to your GTA each of the 3 source code files, on or before the start of

 class on the due date.

 

Be sure to contact me or the TA if you have any questions, and schedule your

time.  Many students will be logged on to omega during the second half of the

semester, sometimes making it difficult for any one additional student to also

log on, as well as decreasing the accessibility to the gcc compiler, lab

printers, etc.

 

Start early, and spend some time each day expanding, compiling, and testing

the code for this assignment.  Also, plan on completing the entire assignment

no later than the Friday before it is due in case OMEGA is down or something

frustrating like that.