Goals: Review understanding and the ability to use arrays.
Part 1 Use pseudo code to describe your add, remove, find, and display options.
Part 2 Answer the following questions
1. What are the inefficiencies associated with using an unordered array if duplicates are not allowed?
- What are the advantages of using an ordered array?
- When would using an unordered array be preferred over using an ordered array?
- Define Big-Oh notation. Explain how z and c in the definition relate to the evaluation of algorithm efficiency.
- Why does computer science often describe algorithms in terms of lginstead of ln?
- How do you convert ln(x) to lg(x)?
- Give an example of algorithm with Big-Oh values of: N2, N lgN, 1, and N.
- Why is Big-Oh 2N represent an algorithm called intractable?
9. How much slower would a linear search be on 1024 items than 16 items?
- How much slower would a binary search be on 1024 items than on 16 items?
Part 3 Implement the program.
- For all our labs, use the Eclipse IDE. On your own computer, you will need to install MinGW.You will also need to download and install the C development toolkit (CDT). There are many tutorials on the Web to step you through this process. Eclipse is a popular IDE for C development; it includes a good interactive debugger and project control
- Write a C program that will maintain a list of employees using either ordered or unordered arrays. Each employee in the list should store an identifier number (integer), a name (String – char array), and a salary (Double). Allow operations to add, remove, display single, display list, reset the list, create ainitial list with randomly populated items, help, and exit.
- You can choose either an unordered list or an ordered list for your implementation. If you choose an unordered list, sort it before display. For an ordered list, use a binary search instead of a linear search.
- You don’t need to implement a GUI for this project. Just create a menu of selections as listed below:
1) Add Employee
2) Remove Employee
3) Display Employee
4) Display Employee List
5) Reset List
6) Create Initial List
Note: The sort option is unnecessary if you implement an ordered list.
- To add an employee, randomly pick an integer employee number that must be unique. The C function rand() can be used. A list of available employee numbers must also be maintained. Initially, this list can be filled with a sequential list of integers. To add a new employee, randomly pick one of these integers and replace its entry in the list with the final entry. Ask the user for an Employee name and salary. Keep track of the number of item numbers that are available using an integer counter.
- When you remove an employee, the employee number should be returned to the free list.
- Make sure to validate that the salary is within a reasonable range. You can use the C scanffunction. Recall that scanf(“%s*”) is needed to clear the input buffer after an error. Always output appropriate error messages when the user types in something illegal. Robust programs should never crash.
- My implementation contained three files; one for the main program (lab1.c), one for the list of employees (EmployeeList.c), and one for maintaining each employee (Employee.c). I also created a header file (empstructs.h), which contained a structure for the employee items and various symbolic constants (see below).
#define MAX_EMPLOYEES 100
#define EMP_SIZE 30
#define LINE 80
#define OPTIONS 9
#define NULL ((void *) 0)
#define FALSE 0;
#define TRUE 1;
/* Declared in Employee.c */
EMPLOYEE* createEmployee(char*, double, int);
char* employeeToString(EMPLOYEE*, char*);
/* Declared in EmployeeList.c */
EMPLOYEE* insert(EMPLOYEE_LIST*, char*, double);
int delete(EMPLOYEE_LIST*, int);
EMPLOYEE* find(EMPLOYEE_LIST*, int);
int traverse(EMPLOYEE_LIST*, void (*)(EMPLOYEE*, char*));
#endif /* EMPSTRUCTS_H_ */
- When you create a new employee, use the malloccommand to allocate the appropriate amount of memory. sizeof(EMPLOYEE), assuming your employee structure is called EMPLOYEE, returns the number of bytes needed for the malloc command. When you remove an employee, or if you are resetting the employee list, make sure to call C’s freefunction when getting rid of employees. C does not include garbage collection, so failing to do this leads to memory leaks.
- The Create option will use a ‘for’ loop to automatically add items to the employee list. Random non-duplicate item number values are required (as we described above). Use any algorithm that you choose to generate different values for the names and salaries; duplicate field values are ok. A simple way to choose employee names is to declare an array of possible names. The following statement can then be used to randomly choose a description:
name = names[(int)(Math.Rand()%namesSize)];
For the salary field, (int)(Math.Rand()%RANGE) + MINSALARY can be used to guarantee that a valid dollar and cents value is used.
- The help command redisplays the menu. Otherwise, it should only display once when the program starts.
- The C puts and printffunctions are quite useful for output. However, make sure to follow the output with a call to fflush(stdout). Otherwise, nothing shows until the output is long enough.
- Eclipse provides lots of warning messages, thatcan save debugging time, but sometimes can cause difficulty in getting rid of the warnings and messages. Your final project should be free of all warnings. Sometimes, you will need to rebuild the project, or clean it, for errors to disappear.
- To copy a substring, use strcpy, but be sure to add a null at the end (‘\0’) of the string. To truncate a string, simply store the null character at the appropriate index.
- Recall that C uses NULL, not the nullof Java.
- To use scanfto limit the size of an input string to 30 characters, use the template “%30s”.
- Standard C does not allow declaring variables in a loop. So for(count=0; count<max; count++), the integer countmust be declared outside the loop.
- Remember that C has no classes, no exceptions, and no function overloading.
- C often lets you do things that crash the program when it runs; so be careful.