QuestionQuestion

Objectives:
a. Familiarity with structures and unions.
b. Declaring variables as sub elements using bit fields
c. Formatting output using printf and sprint funcrtions
d. Using string functions (e.g., strcpy(), strcmp())
e. Pass by reference (an address)
f. Experiment with code reuse

Additional information
1) Short document on bit fields
2) Sample code for data i/o formatting – the code provides examples of formatting output including left and right justification, spaces, floating point formats etc.

Coding Instructions:
1. Comments in Code – as provided in the slides given in class
2. No usage of global variables. All data must be passed or received via function parameters.
3. Write short and simple functions.
4. Use separate files – 3 C (*.c) files and four header files (*.h). The assignment provides the header files and the c files with suggested functions names and prototypes. You do not need to use them if you want to design your software differently. Do not use more files then those provided and do not change the file name. To compile the code use gcc hospital.c patient.c employee.c populateRecords.c
4.1. File hospital.c – containing the main program (containing main(argc, argv)) and the menu functionality. The main function is given with a function to populate the array.
4.2. Files patient.c and patient.h – file patient.c will contains all functions for handling patient records (e.g., printing data of a patient record or search a patient by name). File patient.h will contain the prototype of the patient functions.
4.3. Files employee.c and employee.h – file employee.c will contain all functions for handling employee reocrds (e.g., printing an employee reocord). File employee.h will contain the prototype of theemployee functions. Suggested functions prototypes are given in employee.h but you do not need to use them.
4.4. File struct.h – a file containing the declaration of the three structures (patient, employee and person). It also contain some #define
statements for the different constants that you may find useful in the code.
4.5. Files populateRecords.c and populateRecords.h – this files are already coded. They are used to populate the array person.

Background
You are tasked to create a program for manipulating records of patients and employees in a hospital. The program would allow hospital administrators to query the data and to obtain some summary information. The data of patients and employees is initialized using the function populateRecords(). The data is stored in an array, and the size of the array is given using the define NUM_RECORDS.
The patients and employees data:
a. Shared data:
First Name – first name Family Name – family name,
Telephone – telephone number
b. Employee only data:
Salary – annual salary
Years of service – number of years at the hospital Position – the position at the hospital
Department – the department that the doctors is working at.
c. Patient only data:
Department - the department the patient is in
Daily cost – cost of having the patient in the hospital for one day Number of days – number of days that the patient is hospitalized Severity – the seriousness of the patient illness.
The following information defines the records fields:

Common fields
• First Name – 14 characters long (not including the „\0‟ character)
• Family Name – 14 characters long (not including the „\0‟ character)

Patient fields use bit fields to minimize the size
• Department – an integer in the range of 1-6
• Daily cost – an integer number in the range of 1-50
• Number of days in hospital – an integer number in the range of 0-30
• Severity – an integer number in the range of 0-3

Employee fields use bit fields to minimize the size
• Salary – a real non negative number
• Years of service – an integer in the range of 0-60
• Position – an integer in the range of 0-3
• Department - range 1-6 (integer)

Tasks
In this assignment you will write a short program to manipulate person data of a hospital: patients and employees).

1) Suggestions –
• As you code your small helping functions write small test functions to ensure that the code is correct. This will allow you to focus on the logic of your program without worrying about the simple functions. In particular make sure that you have a few functions that check the validity of the input.
• Create function for each menu option. The function should accept as input the array of person and the number of elements in the array.

Tasks
1. Modifying patient, employee, and person structures

Initial design of the data structures determined that three data structures are sufficient: PatientRec - patient specific data, EmployeeRec - employee specific data and, PersonRec – person data that is commone to both.

// structure contains patient information typedef struct patient {

} PatientRec;

// structure contains employee information typedef struct employee {

unsigned long department; // department in hospital
} EmployeeRec;

// structure contains person information typedef struct person {
char firstName[NAME_SIZE];
char familyName[NAME_SIZE];
char emplyeeOrPatient;
union {
EmployeeRec emp;

};
} PersonRec;

The type of many of the fields does not correspond to the value that a field must hold. Examples are:
• Family Name – in PersonRec the family name can be at most 14 characters long. Storing the „\0‟ character with the family name means that only 15 characters are required for the field familyName. However, the designer allocated 64 character to the family name (the designer set NAME_SIZE to

64). Here you can reduce the size by setting the NAME_SIZE to 15
• In PatientRec the severity field can have values 0-3 which require only 2 bits. However, the initial design set the field severity to an int which is 32 bits. Here you can reduce the size to 2 bits for this field

In this task you need to modify each structure so that:
• Each field can store all the values in the required range
• The structure requires as little memory space as possible.

In order to achieve it you can:
• Change the type of each of the fields in the structure
• Change the order of each of the fields in the structure
• Use bit fields to create subtypes of the integer family of data types - char, short, int or long (either signed or unsigned).
You cannot change the fields names, or the structures names.

2. Populating the array
The program creates an array of 20 persons. It does it by invoking the function populateArray() in file populate_array.c (file populate_array.h contains that function prototype). Note that you will have to link the file with your code.

3. Menu
Create a menu function that will allow the user to manipulate the lists.
3.1. Create a menu function (int menu()) which will return the menu option that the user has selected.
3.2. Display the menu options:
3.2.1. 1. Print all employees
3.2.2. 2. Print all patients
3.2.3. 3. Search patient using Family Name
3.2.4. 4. Summary of Employees Data
3.2.5. 5. Summary of Patients data
3.2.6. 6. Size of structures (PersonRec, PatientRec and EmployheeRec)
3.2.7. 0. Quit
3.3. The program will display the menu to the user and ask the user to enter an option. If the selected option is valid that the system will execute the option. Otherwise, the system will prompt the user that the option is not valid and then redisplay the menu.

4. Menu actions
4.1. Switch Statement

Create a switch statement in the main function, which will process the menu item selected by the user. Each “case” will invoke a corresponding function that will execute one of the actions described in 4.2 – 4.8.
Here you will be responding to the action selected by the user as follows
4.2. Print All Employees
Print all the employees. Hints – a. create a function to print a single employee then call the function in a “for loop” checking if the record is an employee record; b. use sprintf to print the first name and the last name to a temporary string. Then using the temporary string print the name using the formatting.
The “xxx” in the print format represent the number of characters that the printed value must occupy. For example depts.: is followed by xx. Here the dept: number has to occupy 2 spaces. If the value does not have 2 digits (e.g., 14) but rather one digit (e.g., 1) than a space must be used the 1such as dept: 1
Print format
First Name Family Name (33 char) dept:xx salary:xxxxxx.xx position:xx years of service:xxxx salary to-date:xxxxxxxx.xx For example:

Printout example
Hospital Employees

4.3. Print all Patients
Print all the patients. Hints – a. create a function to print a patient then use the function is a “for loop” chcking each record if it is an employee record; b. use sprintf to print the first name and the last name to a temporary string. Then using the temporary string print the name using the formatting.
Print format
First Name Family Name (33) characters dept:xx days in hospital:xxx severity:xx daily cost:xxx total cost:xxxxx

Output example

Patient List
John Johnson dept: 3 days in hospital: 21 severity: 0 daily cost: 32 total cost: 672
David Carp dept: 5 days in hospital: 26 severity: 1 daily cost: 37 total cost: 962

4.4. Print Patients by Family Name
4.4.1. Prompt the user to enter a family name
4.4.2. Search the list for all patients with a matching family name and print their record as above (Section 4.3)

4.5. Summary of Employees Data – here you will provide a summary data of the employees in the hospital
The summary will consist of two parts: overall summary and summary by position.
4.5.1. Overall summary
Here the program will provide the number of employees in the hospital, the total salary that is paid to the employees and the average salary
The output format is
Total number of employees:xxx total salary:xxxxxxx.xx average salary:xxxxx.xx For example:
Total number of employees: 11 total salary: 248450.75 average salary:22586.43

4.5.2. Summary by position
The hospital has four postions 0, 1, 2, and 3. Here you will provide a summary of the employees by positions. For each position you will provide the number of employees in the position, the total salary of the employees in the position and the average salary in the position.
The format is:
position[0] – employees:xx total salary:xxxxxx.xx average salary:xxxxx.xx

For example
position[0] - employees: 2 total salary: 21997.44 average salary:10998.72

An example of the output is:
Employee Summary
Total number of employees: 11 total salary: 248450.75 average salary:22586.43

Summary by position
position[0] - employees: 2 total salary: 21997.44 average salary:10998.72 position[1] - employees: 3 total salary: 91936.06 average salary:30645.35 position[2] - employees: 3 total salary: 70829.72 average salary:23609.91 position[3] - employees: 3 total salary: 63687.52 average salary:21229.17

4.6. Summary of Patient Data – here you will provide a summary data of the patients in the hospital
The summary will consist of two parts: overall summary and summary by department.

4.6.1. Overall summary
Here the program will provide the number of patients in the hospital, the total cost to date of caring for the patients and the average cost per patient. The total cost to date is the sum of caring for all patients (daily cost per patient x number of days in hostpital)
The output format is
Total number of patients:xxx total cost to-date:xxxxxxx.xx average cost per patient to-date:xxxxx.xx For example:
Total number of patients: 9 total cost to-date: 6052.00 average cost per patient to-date: 672.44

4.6.2. Summary by Department
The hospital has six departments 1, 2, 3, 4, 5, 6. Here you will provide a summary of the patients by department. For each department you will provide the number of patients, the total cost of caring for the patients in the department to date, the daily cost of caring and the average daily cost per patient. The cost to-date is the sum of the caring for each patient (number of days in the hospital x daily cost). The daily cost is the sum of daily cost for all patients in the department.
The format is:
Department [1] - patients:xxx cost to-date:xxxx.xx daily cost:xxxx.xx average daily cost per patient:xxxx.xx

For example

Department [1] - patients: 1 cost to-date: 234.00 daily cost: 26.00 average daily cost per patient: 26.00

An example of the output is:
Patient Summary
Total number of patients: 9 total cost to-date: 6052.00 average cost per patient to-date: 672.44

Summary by Department
Department [1] - patients: 1 cost to-date: 234.00 daily cost: 26.00 average daily cost per patient: 26.00 Department [2] - patients: 1 cost to-date:1029.00 daily cost: 49.00 average daily cost per patient: 49.00 Department [3] - patients: 2 cost to-date:1730.00 daily cost: 78.00 average daily cost per patient: 39.00

Department [5] - patients: 3 cost to-date:1487.00 daily cost: 63.00 average daily cost per patient: 21.00 Department [6] - patients: 2 cost to-date:1572.00 daily cost: 87.00 average daily cost per patient: 43.50

4.7. Size of structures (PersonRec, PatientRec and EmployheeRec)
Print the size of the three structures Format to follow:
Size of structures
Size of PersonRrec = 148 Size of EmployeeRrec = 16 Size of PatientRrec = 12

Note that the above example is for the provided structures. They do not reflect the minimization of the structures.
4.8. Quit
4.8.1. Warn the user that he/she is about to quit and ask whether to proceed (y/n)

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include "employee.h"
#include "stdio.h"

/**************************************************************/

/* print a employee
input
* person: a person
output nothing
*/
void printEmployee(PersonRec person) {

    // add code

    /*
    * use sprintf to print the first name
    * and the last name to a temporary string.
    * Then using the temporary string print the name using the formatting.
    */
    char name[33];
    sprintf(name, "%s %s", person.firstName, person.familyName);
    printf("%-33s dept:%2u salary:%9.2f position:%2u years of service:%4u salary to-date:%11.2f",
            name, person.emp.department,
            person.emp.salary, person.emp.position,
            person.emp.yearsService,
            person.emp.salary * person.emp.yearsService);
}

/**************************************************************/

/* print all employee in the array
input
* person: array of person,
* numRecords: size of the array
output
*/
void printEmployees(PersonRec *person, int numRecords) {
    // add code
    // in a “for loop” checking if the record is an employee record
    int i;
    for (i = 0; i < numRecords; i++) {
       if (person[i].emplyeeOrPatient == EMPLOYEE_TYPE) {
            printEmployee(person[i]);
            printf("\n");
       }
    }
}...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$60.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats