|
|
|
|
|
|
|
Problem-Solving Case Study An Algorithm for a Company Payroll |
|
|
|
|
|
|
|
|
Problem: A small company needs an interactive program (the payroll clerk will input the data) to figure its weekly payroll. The input data and each employee's wages should be saved in a secondary storage file, and the total wages for the week should be displayed on the screen so that the payroll clerk can transfer the appropriate amount into the payroll account. |
|
|
|
|
|
|
|
|
Discussion: At first glance, this seems like a simple problem. But if you think about how you would do it by hand, you see that you need to ask questions about the specifics of the process: What employee data is input? How are wages computed? In what file should the results be stored? How does the clerk indicate that all of the data has been entered? |
|
|
|
|
|
|
|
|
The data for each employee include an employee identification number, the employee's hourly pay rate, and the hours worked that week. |
|
|
|
|
|
|
|
|
Wages equal the employee's pay rate times the number of hours worked up to 40 hours. If an employee worked more than 40 hours, wages equal the employee's pay rate times 40 hours, plus one and a half times the employee's regular pay rate times the number of hours worked above 40. |
|
|
|
|
|
|
|
|
The results should be stored in a file called payFile. |
|
|
|
|
|
|
|
|
There is no employee number 0, so the clerk can indicate the end of the data by entering a 0 when asked for an employee number. |
|
|
|
|
|
|
|
|
Let's apply the divide-and-conquer approach to this problem. There are three obvious steps in almost any problem of this type: |
|
|
|
|
|
|
|
|
First we need to get the data. (By get, we mean read or input the data.) We need three pieces of data for each employee: employee identification number, hourly pay rate, and number of hours worked. So that the clerk will know when to enter each value, we must have the computer output a message that indicates when it is ready to accept each of the values (this is called a prompting message, or a prompt). Therefore, to input the data, we take these steps: |
|
|
|
|
|
|
|
|
Prompt the user for the employee number (put a message on the screen)
Read the employee number
Prompt the user for the employee's hourly pay rate
Read the pay rate
Prompt the user for the number of hours worked
Read the number of hours worked |
|
|
|
|
|