C++/help with two problems
Expert: Eddie - 2/25/2005
QuestionI have two problems that I'm confused with so I need help during the format for both problems.
The problems are: (1) I have to write a function called INVERSESUM, which returns the inverse sum of two real numbers, that is, 1/(x+y). I have to prompt the user for x and y and print the inverse sum.
Problem 2. I have to write a function for each of the following:
a) federal tax deductions, 15% of salary
b) insurance deductions, based upon a code
i. 1 indicates 5% of salary
ii. 2 indicates 8% of salary
iii. 3 indicates 0% of salary
c) state taxes 7.5% of salary
d) overtime pay, 1.5 more of hourly rate for hours worked over 40
The main program inputs data from a data file that contains an employee's number, hours worked, hourly rate, and insurance code. If the number of hours is greater than 40 than overtime pay is to be calculated. The main program calculates the regular pay as
Regular Pay = Hours worked * Hourly rate
And calls all other functions as needed.
Print the employee number, hours worked, hourly rate, overtime pay (if any), all deductions, and net and gross salaries in tabular form.
Net Salary is salary after deductions
Gross Salary is salary before deductions
AnswerHello antonio, thank you for the question.
I'm not sure why you asked me, it seems you've already written the code for these.
1. float InverseSum(float x, float y)
{
return 1 / (x + y);
}
That's all there is to the first problem. The second is a little bit more complicated. Though it's nothing more than a little bit of math.
2.
a.
float Federal(float salary)
{
return salary * .85f;
}
b.
float Insurance(float salary, int code)
{
switch(code)
{
case 1:
return salary * .95f;
break;
case 2:
return salary * .92f;
break;
case 3:
return salary;
break;
default:
cout << code << " is not a valid code\n";
return;
break;
}
}
c.
float State(float salary)
{
return salary * .925f;
}
d.
float Overtime(float hWorked, hRate)
{
if(hWorked > 40.0f)
return hWorked * (hRate * 1.5f);
return hWorked * hRate;
}
See, that wasn't so bad. Each function returns the adjusted salary. For example: salary = Overtime(salary); would produce the money including the overtime. The rest is just merely reading in values, calling the functions, and printing the adjusted values back out to the console.
I hope this information was helpful.
- Eddie