In: Computer Science
using c++. i always grade my answers
The variables x, y, z, rate, and hours referred to in the bullets below are the variables of the function main. Each of the functions described must have the appropriate parameters to access these variables. Write the following definitions:
Write the definition of the value-returning function paycheck that calculates and returns the amount to be paid to an employee based on the hours worked and rate per hour. The hours worked and rate per hour are stored in the variables hours and rate, respectively, of the function main.
The formula for calculating the amount to be paid is as follows:
For the first 40 hours, the rate is the given rate; for hours over 40, the rate is 1.5 times the given rate.
An example of the program is shown below:
After initialization: x = 0, y = 0, z = Enter hours worked: 60 Enter pay rate: 20 Hours worked: 60 Pay Rate: $20 This week's salary: $1400 Before calling funcOne: x = 35, y = 20 Enter an integer: 2 After funcOne: x = 88 z = B After nextChar: z = C
Program:
#include <iostream>
#include <math.h>
using namespace std;
void getHoursRate(int &hour, float &r);
void initialize(int &xR,int &yR,char &zR);
float paycheck(int &payHour, float &payRate);
void printCheck(int &h,float &rT,float &amt);
void funcOne(int &oneX,int &oneY);
void nextChar(char &zNext);
int main()
{
float rate;
int hours,x,y;
char z;
initialize(x,y,z);
cout<<endl<<"After initialization "<<"x=
"<<x<<" y= "<<y<<" z=
"<<z<<endl;
getHoursRate(hours,rate);
float amount = paycheck(hours,rate);
printCheck(hours,rate,amount);
funcOne(x,y);
cout<<endl<<"After funcOne: x= "<<x;
z = 'B';
cout<<endl<<"z= "<<z;
nextChar(z);
cout<<endl<<"After nextChar z= "<<z;
return 0;
}
void initialize(int &xR,int &yR,char &zR)
{
xR = 0;
yR=0;
zR = ' ';
}
void getHoursRate(int &hour, float &r)
{
cout<<"Enter hours worked: ";
cin>>hour;
cout<<"Enter pay rate: ";
cin>>r;
}
float paycheck(int &payHour, float &payRate)
{
float salary = 0;
if(payHour>40)
{
salary = salary + ((payHour - 40) * (payRate * 1.5));
salary = salary + ((payHour - (payHour- 40))* payRate);
}
else
{
salary = salary + (payRate * payHour);
}
return salary;
}
void printCheck(int &h,float &rT,float &amt)
{
cout<<endl<<"Hours worked: "<<h;
cout<<endl<<"Pay Rate: $"<<rT;
cout<<endl<<"This week's salary: $"<<amt;
}
void funcOne(int &oneX,int &oneY)
{
int number;
oneX = 35;
oneY = 20;
cout<<endl<<"Before calling funcOne: x =
"<<oneX<<", y = "<<oneY;
cout<<endl<<"Enter an integer: ";
cin>>number;
oneX = (oneX * 2) + oneY - number;
}
void nextChar(char &zNext)
{
zNext = zNext + 1;
}
Output: