Previous | Next | Table of Contents | Index | Program List | Copyright

5.3 Problem Solving: Repeating a Program Body

In the discussion of repetition in programs, we mentioned that we would like to be able to execute the payroll program for several employees in a single run. We will see how to do this next.


Case Study: Multiple-Employee Payroll Problem

PROBLEM SPECIFICATION

Modify the payroll program to compute gross pay and net pay for a group of employees.

ANALYSIS

The number of employees must be provided as input data along with the hourly rate and hours worked by each employee. The same set of variables will be used to hold the data and computational results for each employee. The computations will be performed in the same way as before.

Data Requirements

Problem Constants

maximum salary for no tax deduction (TaxBracket = 100.0)

amount of tax deducted (Tax = 25.00)

maximum hours without overtime pay (MaxHours = 40.0)

Problem Inputs

number of employees (NumEmp : Positive)

hours worked by each employee (Hours : NonNegFloat)

hourly rate for each employee (Rate : NonNegFloat)

Problem Outputs

gross pay (Gross : NonNegFloat)

net pay (Net : NonNegFloat)

DESIGN

Algorithm

1. Prompt for the number of employees (NumEmp).

2. FOR each employee LOOP

     Enter payroll data and compute and print gross and net pay.

END LOOP;

An additional variable is needed to count the number of employees processed and to control the FOR loop in step 2.

Program Variable

loop counter--counts the employees that are processed:

(CountEmp : Positive)

The structure chart is shown in Figure 5.1. (The structure chart for the subproblem "find gross and net pay" was drawn in Figure 4.3.)

Figure 5.1
Structure Chart for Multiemployee Payroll Program

Figure 5.1

IMPLEMENTATION:

Program 5.7 gives the entire program. Notice how the code is very similar to that in the original program, with the addition of a few more declarations and the loop construct. Sample output is given for three employees.

Program 5.7
Multiemployee Payroll Program

WITH Ada.Text_IO;
WITH Ada.Integer_Text_IO;
WITH Ada.Float_Text_IO;
PROCEDURE Multi_Payroll IS
------------------------------------------------------------------------
--| Computes and displays gross pay and net pay for a number
--| of employees, given each employee's hourly rate and hours worked.
--| Deducts a tax of $25 if gross salary exceeds $100;
--| otherwise, deducts no tax.
--| Author: Michael B. Feldman, The George Washington University
--| Last Modified: July 1995
------------------------------------------------------------------------

  SUBTYPE NonNegFloat IS Float RANGE 0.0 .. Float'Last;

  TaxBracket : CONSTANT NonNegFloat := 100.00;
                             -- maximum salary for no deduction
  Tax :        CONSTANT NonNegFloat := 25.00;   -- tax amount

  NumEmp: Positive;    -- input - number of employees

  Hours : NonNegFloat; -- inputs - hours worked, hourly rate
  Rate:   NonNegFloat;

  Gross:  NonNegFloat; -- outputs - gross pay, net pay
  Net:    NonNegFloat;

BEGIN -- Multi_Payroll

  Ada.Text_IO.Put ("Please enter number of employees > ");
  Ada.Integer_Text_IO.Get (Item => NumEmp);

  FOR CountEmp IN 1 .. NumEmp LOOP

    -- Enter Hours and Rate
    Ada.Text_IO.Put (Item => "Employee number ");
    Ada.Integer_Text_IO.Put (Item => CountEmp, Width => 1);
    Ada.Text_IO. New_Line;
    Ada.Text_IO.Put (Item => "  Hours worked > ");
    Ada.Float_Text_IO.Get (Item => Hours);
    Ada.Text_IO.Put (Item => "  Hourly rate > ");
    Ada.Float_Text_IO.Get (Item => Rate);

    -- Compute gross salary
    Gross := Hours * Rate;

    -- Compute net salary
    IF Gross > TaxBracket THEN
      Net := Gross - Tax;     -- Deduct a tax amount
    ELSE
      Net := Gross;           -- Deduct no tax
    END IF;

    -- Print Gross and Net
    Ada.Text_IO.Put (Item => "  Gross salary is $");
    Ada.Float_Text_IO.Put (Item => Gross, Fore => 1, Aft => 2, Exp => 0);
    Ada.Text_IO.Put (Item => "; Net salary is $");
    Ada.Float_Text_IO.Put (Item => Net, Fore => 1, Aft => 2, Exp => 0);
    Ada.Text_IO.New_Line;

  END LOOP;

END Multi_Payroll;
Sample Run
Please enter number of employees > 3
Employee number 1
  Hours worked > 40
  Hourly rate > 6.50
  Gross salary is $260.00; Net salary is $235.00
Employee number 2
  Hours worked > 37.5
  Hourly rate > 7.25
  Gross salary is $271.88; Net salary is $246.88
Employee number 3
  Hours worked > 39.5
  Hourly rate > 6.50
  Gross salary is $256.75; Net salary is $231.75


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.