#include <stdio.h>
int main() {
const int a = 1200;
int b = 40;
int c = 250;
int hour;
printf("Enter total hours\n");
scanf("%d", &hour);
switch(hour){
case 1:
if(hour==40)
printf("Your week pay is:%d.\n",a);
break;
case 2:
if(hour>40)
printf("Your week pay is: %d.\n", (hour-40)*b);
break;
case 3:
if(hour>55)
printf("Your week pay is: %d.\n", a ((hour-40)*b) c);
break;
}
return 0;
}
I'm computing for weekly salary for an hourly employee, the employee have a regular hourly rate of $30 and have an overtime rate of $40. The first 40 hours are paid using regular rate and the succeeding hours are paid using the overtime rate. There is also the hazard pay which when the employee works more than 55 hours their is additional $250.
CodePudding user response:
Math?
This is what the programme is attempting to do; it will not work, anyways, because it is incorrect in it's given form. We are given a stepwise-rate linear function. Given y
, the payment amount, and x
, the hours worked, It should:
- intersect the origin;
- be composed of straight lines; C2 should be 0 everywhere it's differentiable;
- have a kink upwards at 40h;
- have a discontinuity at 55h;
- be strictly monotonic, (one would hope at least minimum wage.)
From the problem, the cases are: 0h <= x <= 40h, 40h < x <= 55h, 55h < x. Apparent corner cases are 0h, 40h, and 55h, at least, that one should figure out on paper beforehand to test this programme, (I'd throw in 41h, 56h, and 60h.) Doing a quick sketch of the graph on paper will probably be helpful.
Putting it into a programme
A switch
statement is inappropriate because that's testing equality; this is a range of values, so this will be better suited to be a series of if
blocks. Since the values are monotonically increasing, one could build up the pay,
pay = 0
if 55 < x
pay = bonus
if 40 < x
pay = ...
x = 40
...
Or, as you have noticed, the section of 40h pay is a constant over 40h, one can separate the cases using the integral, $30/h * 40h = $1200
, and calculate it directly using a shifted x
.
if 55 < x
pay = bonus ...
else if 40 < x
pay = 1200 ...
else
pay = ...
CodePudding user response:
There is no need to use a switch statement.
Your code will never show any output.
You can use if/else
if/else if/else
. So, remove the switch statement and Try this:
#include <stdio.h>
int main() {
const int a = 1200;
int b = 40;
int c = 250;
int hour;
printf("Enter total hours\n");
scanf("%d", &hour);
if(hour > 55)
printf("Your week pay is: %d.\n", a ((hour-40)*b) c);
else if(hour > 40 )
printf("Your week pay is: %d.\n", a (hour-40)*b);
else
printf("Your week pay is:%d.\n", a);
return 0;
}