For this assignment you will write a function that calculates an amount of pay for an employee. The function will take a number of hours worked and a rate of pay as parameters, and return the amount of pay the employee will receive for their work.
For full credit your program must pass the tests, and be organized according to all of the following requirements.
The organization of your program must follow the structure of our example program in class (example 06 folder posted to GitHub) which utilizes function prototypes. That is, the prototype of your calculate_pay()
function must be come before main()
and the implementation of calculate_pay()
must come after main()
.
The calculate_pay()
function must compute the amount of pay an employee will receive for working a certain number of hours at a certain rate. calculate_pay()
must not do any printing, main()
will print the value that calculate_pay()
returns.
Here is the prototype:
double calculate_pay(double hours, double rate);
where hours
is the number of hours worked and rate
is the hourly rate of pay. The function returns the amount of pay the employee should receive.
Sidenote: In general it is not a good idea to use a double variable to store monetary values due to rounding errors, but for the purpose of simplicity for this assignment we will use double variables.
Often if an employee works more than 40 hours in a week they will receive overtime pay which is 1.5 times the regular pay. Overtime pay is only given for the hours past 40 that the employee worked. For example, if an employee makes $10/hour and worked for 50 hours, they earn $400 for the first 40 hours and $150 for the next 10 hours for a total of $550.
Your main()
function must ask the user for a number of hours and a rate of pay, use the calculate_pay()
function to calculate the pay using the values supplied by the user, and then print out the pay. You must ask for the number of hours worked first and the rate of pay second. Remember that you need to use %lf
as the conversion specifier for double variables when using scanf()
. For printf()
you can use either %lf
or %f
with a double, but my preference is for %lf
since it is more explicit and maintains consistency with scanf()
.
You must print out the amount of pay with a dollar sign and 2 decimal places of precision. You can accomplish that by adding .2 to the conversion specifier like this:
printf("You earned $%.2lf\n", amount_of_pay);
Make sure you print out the newline!
Here is an example of what one run of your program should look like:
Enter hours worked: 45.5
Enter the rate of pay: 10
You earned $482.50