Annual Pay

This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (2) was titled “Introduction to C++”.

2.6: Suppose an employee gets paid every two weeks and earns $1700.00 each pay period. In a year the employee gets paid 26 times. Write a program that defines the following variables:

  • payAmount This variable will hold the amount of pay the employee earns each pay period. Initialize the variable with 1700.0.
  • payPeriods This variable will hold the number of pay periods in a year. Initialize the variable with 26.
  • annualPay This variable will hold the employee’s total annual pay, which will be calculated.

The program should calculate the employee’s total annual pay by multiplying the employee’s pay amount by the number of pay periods in a year, and store the result in the annualPay variable. Display the total annual pay on the screen.

My solution (using hard-coded values as per instructions):

/* Chapter 2 Challenge 6
Written by Kirk Hingsberger
September 13, 2007
Annual Pay challenge
*/

#include
using namespace std;

int main()
{
float payAmount = 1700.0;
float payPeriods = 26;
float annualPay = payAmount * payPeriods;

cout << "The employee worked " << payPeriods << " pay periods" << endl;
cout << "at the per period wage of $" << payAmount << endl;
cout << "therefore earning an annual total $" << annualPay << endl;

return 0;
}

As with many other beginning C++ programs, we were instructed to not sweat the fact that our figures computed in scientific notation or with many decimal places. We would learn those controls later.

Here is the capture script I turned in for grading:

Script started on Thu 13 Sep 2007 10:14:41 AM MDT
$ cat chap2chlg6.cc
/* Chapter 2 Challenge 6
Written by Kirk Hingsberger
September 13, 2007
Annual Pay challenge
*/

#include
using namespace std;

int main()
{
float payAmount = 1700.0;
float payPeriods = 26;
float annualPay = payAmount * payPeriods;

cout << “The employee worked ” << payPeriods << ” pay periods” << endl;
cout << “at the per period wage of $” << payAmount << endl;
cout << “therefore earning an annual total $” << annualPay << endl;

return 0;
}
$ g++ chap2chlg6 -o chap2chlg6.cc -s
$ chap2chlg6
The employee worked 26 pay periods
at the per period wage of $1700
therefore earning an annual total $44200
$ exit
exit

Script done on Thu 13 Sep 2007 10:15:19 AM MDT

  • Share/Bookmark




Leave a Reply