Starting from:

$35

Assignment 4 program that manages employees

ECE-C301 Programming for Engineers

Programming Assignment 4
1 The Big Idea
In this assignment you will be writing a simple program that manages employees. Your program will be able
to perform the following simple actions:
• Add an Employee Record – This action adds an employee record. Each employee record consists
of the employee’s name, age, and hourly wage.
• Delete an Employee Record – This action deletes an existing employee record. Attempting to
delete an invalid or non-existent record must return an error message without crashing the program.
• List All Employees in order of Ascending Age – This action lists all of the employee records
from youngest to oldest.
• Quit – This action quits the program.
2 The Employee Record
An employee record is simply a struct employee, which has the following form:
struct employee {
char name[128];
unsigned int age;
unsigned int wage;
};
This structure can be found in the provided header file employee.h.
Your program will be primarily concerned with maintaining an array of struct employee entries. This can
be seen in the primary program logic provided in menu.c. Because we will be using an array of struct
employee entries, the number of employees we can store will be limited to the number of elements in the
array. Consequently, we will be unable to actually delete employee entries–the entire array of employee entires
must be allocated throughout the duration of the program’s execution. We will learn how to overcome this
limitation in the future. However, for now, we must agree on a method to invalidate an employee entry–such
invalidated entries will be said to be “deleted”. For this assignment, we will mark an employee entry as
invalidated (i.e. “deleted”) if its first byte is NULL (i.e. ‘\0’). As we will discuss later, we will keep our
array sorted such that all valid entires are first in the array with invalid entries following. In other words,
much like a C string, our struct employee array will be NULL terminated.
3 Getting Ready: Basic Program Organization
The program structure itself is provided to you in menu.c. As you can see, it is a fairly simple program that
fits entirely on a single 8.5×11” page:
1
1 #include <stdio.h
2 #include <string.h
3 #include "employee.h"
4
5 #define MAX 100
6 #define OPTION_ADD 1
7 #define OPTION_DEL 2
8 #define OPTION_LIST 3
9 #define OPTION_QUIT 4
10
11 unsigned int print_menu ()
12 {
13 unsigned int action;
14
15 printf(" -= MENU =-\n");
16 printf("[1] Add New Employee\n");
17 printf("[2] Delete an Employee\n");
18 printf("[3] List All by Age (Acending)\n");
19 printf("[4] Quit\n");
20 printf("------------------------\n");
21 printf("Selection: ");
22
23 scanf("%u", &action);
24
25 return action;
26 }
27
28 int main (unsigned int argc, char** argv)
29 {
30 struct employee list[MAX];
31
32 unsigned int running = 1;
33
34 /* Set all bits in the employee array to zero */
35 memset (list, 0, MAX*sizeof(struct employee));
36
37 while (running) {
38 switch (print_menu()) {
39 case OPTION_ADD:
40 employee_add(list);
41 break;
42 case OPTION_DEL:
43 employee_delete(list);
44 break;
45 case OPTION_LIST:
46 employee_print_all(list);
47 break;
48 case OPTION_QUIT:
49 running = 0;
50 break;
51 };
52 }
53 return 0;
54 }
2
As you can see, the program can be so sort because it depends heavily on external code and data structures
declared in the header employee.h. As you may have guessed, the implementations for these functions reside
in the provided employee.c, which contains empty functions. It is the goal of this assignment for you to fill
in these empty functions. When you are successful, you must be able to compile and run the program as
follows:
1 $ gcc -o project4 menu.c employee.c
2 $ ./project4
4 Adding Employees
The process of adding employees is fairly straight forward. If the user chooses to add a new employee, they
must be prompted (in this order) for the following information: First Name, Last Name, Age, and Wage.
After this new entry is added to your array of struct employee entires, you must sort your array in order
of ascending age (we will talk about this in the next section. Here is what your program must look like when
adding employees:
1 -= MENU =-
2 [1] Add New Employee
3 [2] Delete an Employee
4 [3] List All by Age (Acending)
5 [4] Quit
6 ------------------------
7 Selection: 1
8 First Name: Robert
9 Last Name: Baratheon
10 Age: 54
11 Wage: 999
12 -= MENU =-
13 [1] Add New Employee
14 [2] Delete an Employee
15 [3] List All by Age (Acending)
16 [4] Quit
17 ------------------------
18 Selection: 1
19 First Name: Donald
20 Last Name: Trump
21 Age: 70
22 Wage: 50
23 -= MENU =-
24 [1] Add New Employee
25 [2] Delete an Employee
26 [3] List All by Age (Acending)
27 [4] Quit
28 ------------------------
29 Selection: 1
30 First Name: Mickey
31 Last Name: Mouse
32 Age: 88
33 Wage: 12
Note: Since you are reading the First and Last names of the employee separately, you will need to concatenate them in order to store the full name as a single string in the provided struct employee data
structure.
3
5 Sorting
You will need to keep your array of struct employee entires sorted at all times. This means that entires
must be ordered in ascending age (youngest first, oldest last) with all invalidated entries following the oldest
employee. Invalidated entires do not need to be stored in any particular order amongst themselves, but they
must all be stored after the sorted valid entries.
To help you in this task, here is an example implementation of a very simple sorting algorithm called “Bubble
Sort” being used to sort a simple array of integers:
1 #include <stdio.h
2
3 int main (int argc, char** argv)
4 {
5 int i, j, n;
6 int array[100];
7 int tmp;
8
9 printf("Enter number of elements\n");
10 scanf("%d", &n);
11
12 printf("Enter %d integers\n", n);
13 for (i=0; i<n; i++)
14 scanf("%d", &array[i]);
15
16 for (i=0; i<(n-1); i++)
17 for (j=0; j<n-i-1; j++)
18 if (array[j] array[j+1]) {
19 tmp = array[j];
20 array[j] = array[j+1];
21 array[j+1] = tmp;
22 }
23
24 printf("Sorted list:\n");
25 for (i=0; i<n; i++)
26 printf("%d\n", array[i]);
27
28 return 0;
29 }
You must sort your array of struct employee entires after any addition or deletion operation. This will
require that you modify the provided sorting code in order to work the more complex struct employee
data structure instead of just simple integers.
4
6 Listing All Employees by Ascending Age
If the user chooses to list all employees, they must be displayed in order of ascending age. For example,
given the employees we added earlier, your program must provide output in the following format:
1 -= MENU =-
2 [1] Add New Employee
3 [2] Delete an Employee
4 [3] List All by Age (Acending)
5 [4] Quit
6 ------------------------
7 Selection: 3
8 --------------
9 Employee #0
10 Name: Robert Baratheon
11 Age: 54
12 Wage: 999
13 --------------
14 Employee #1
15 Name: Donald Trump
16 Age: 70
17 Wage: 50
18 --------------
19 Employee #2
20 Name: Mickey Mouse
21 Age: 88
22 Wage: 12
23 --------------
24 Num Employees: 3
5
7 Deleting an Employee
The process of deleting an employee is fairly tricky! Be very careful and thoughtful when you go about doing
this! Given the interactions we have had with the program thus far, deletion must work as follows:
1 -= MENU =-
2 [1] Add New Employee
3 [2] Delete an Employee
4 [3] List All by Age (Acending)
5 [4] Quit
6 ------------------------
7 Selection: 2
8 Enter ID # to delete: 1
9 ** Employed #1 Deleted**
10
11 -= MENU =-
12 [1] Add New Employee
13 [2] Delete an Employee
14 [3] List All by Age (Acending)
15 [4] Quit
16 ------------------------
17 Selection: 3
18 --------------
19 Employee #0
20 Name: Robert Baratheon
21 Age: 54
22 Wage: 999
23 --------------
24 Employee #1
25 Name: Mickey Mouse
26 Age: 88
27 Wage: 12
28 --------------
29 Num Employees: 2
Note that your program must be smart enough to not attempt the deletion of a non-existent employee. For
example, when trying to delete a non-existent employee 56, your program must provide the following output:
1 -= MENU =-
2 [1] Add New Employee
3 [2] Delete an Employee
4 [3] List All by Age (Acending)
5 [4] Quit
6 ------------------------
7 Selection: 2
8 Enter ID # to delete: 56
9 ** Invalid Selection**
10
11 -= MENU =-
12 [1] Add New Employee
13 [2] Delete an Employee
14 [3] List All by Age (Acending)
15 [4] Quit
16 ------------------------
6
8 Deliverables
Your deliverables for this project are a working program that you wrote – no copying from your friends or
lab mates, please! Also, please submit a short report detailing the project, your implementation, and your
testing results including screen shots (similar to what I have provided for you here).
Submit your report in PDF format.
Submit your complete source code as a zip file.
7

More products