#include <stdio.h> int main(void) { int hourlyWage = 0; int weeklyHours = 0; int
ID: 2084823 • Letter: #
Question
#include <stdio.h>
int main(void) {
int hourlyWage = 0;
int weeklyHours = 0;
int weeklySalary = 0;
int overtimeHours = 0;
const int WEEKLY_LIMIT = 40;
printf("Enter hourly wage: ");
scanf("%d", &hourlyWage);
// FIXME: Get user input value for weeklyHours
weeklyHours = 40;
if (weeklyHours <= WEEKLY_LIMIT) {
weeklySalary = weeklyHours * hourlyWage;
}
else {
overtimeHours = weeklyHours - WEEKLY_LIMIT;
weeklySalary = (int)((hourlyWage * WEEKLY_LIMIT) +
(hourlyWage * overtimeHours * 1.5));
}
printf("Weekly salary is: %d ", weeklySalary);
return 0;
The following program calculates yearly and monthly salary given an hourly wage. The program assumes work-hours-per-week imit of 40 and work-weeks-per-year of 50. Overtime refers to hours worked per week in excess of some weekly limit, such as 40 hours. Some companies pay time-and-a-half for overtime hours, meaning overtime hours are paid at 1.5 times the hourly wage. Overtime pay can be calculated with pseudocode as follows (assuming a weekly limit of 40 hours): weeklyinit = 40 if weeklyHoursweeklyLimit weeklySalary = hourlywage * week!yHours else overtimeHoursweeklyHours - weeklyLimit weeklySalary = hourlyMage * weeklyLinit + (overtimeHours * hourlywage * 1.5) 1. Run the program and observe the salary earned 2 Modify the program to read user input for weeklyHours. Run the program again.Explanation / Answer
1. If we run the program we get the output as follows.
Output
Enter Hourly Wage: 100
Weekly salary is: 40000
Explanation
This is because the valiable 'weeklyHours' is fixed to 40. It is within the maximum weekly limit (WEEKLY_LIMIT=40). Thus any amount of hourly wage you enter the output will be always that amount multiplied by 40.
2. The program modified as per the question. Modified lines highlighted.
#include <stdio.h>
int main(void) {
int hourlyWage = 0;
int weeklyHours = 0;
int weeklySalary = 0;
int overtimeHours = 0;
const int WEEKLY_LIMIT = 40;
printf("Enter hourly wage: ");
scanf("%d", &hourlyWage);
printf("Enter weekly hours: "); // This is modified line -1
scanf("%d", &weeklyHours); // This is modified line -2
// The line weeklyHours = 40; is deleted.
if (weeklyHours <= WEEKLY_LIMIT) {
weeklySalary = weeklyHours * hourlyWage;
}
else {
overtimeHours = weeklyHours - WEEKLY_LIMIT;
weeklySalary = (int)((hourlyWage * WEEKLY_LIMIT) +
(hourlyWage * overtimeHours * 1.5));
}
printf("Weekly salary is: %d ", weeklySalary);
}
Output case -1:
Enter hourly wage: 30
Enter weekly hours: 35
Weekly salary is: 1050
OUtput case-2:
Enter hourly wage: 30
Enter weekly hours: 45
Weekly salary is: 1425
Explanation
Now the code is modified to accept weekly hours as well.
If the weekly hours is within the weekly limit, the salary will be the product of hourly wage and weekly hours (As in output case -1)
If the weekly hours is above the weekly limit, the salary will be the product of hourly wage and weekly limit + 1.5 times of the product of excess hours and hourly wage. (As in output case -1)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.