SOURCE FILE: 038_salaryCalc_C.cpp



/* 
    This program illustrates how to use a "math trick" to solve a problem
    without resortig to the use of if- or if-else-logic.  The problem is to
    calculate pay in a situation where overtime is a possibility.
*/

#include <iostream>
#include <math.h>
using namespace std ;

int main (void)
{
  double rate, hours, total_pay ;
  cout << "What is the hourly rate of pay?: " ;
  cin >> rate ;
  cout << "How many hours did you work this week?: " ;
  cin >> hours ;
  double reg_pay = rate * hours ;
  double ot_hrs = hours - 40 ;
      /*
           The code below employs the "trick"

           If ot_hrs is not positive, this changes its
           value to zero, else it is not changed.
 
           (Of course, values may be slightly inaccurate 
            due to limitations of the computer hardware.)
      */
  ot_hrs = (sqrt(ot_hrs*ot_hrs) + ot_hrs ) /2 ;
  double ot_incr = ot_hrs * rate * 0.5 ;

  total_pay = reg_pay + ot_incr ;

  cout << "Your total pay is " << reg_pay << " plus overtime of "
       << ot_incr << " which sums to " << total_pay << ".\n" ;

  return 0;
}