In this section we will look at a new sample program and see what happens to memory when this program is loaded and then executed.
Example 2.6
A payroll program is shown in Program 2.7.
Program 2.7
WITH Ada.Text_IO; WITH Ada.Float_Text_IO; PROCEDURE Weekly_Pay IS ------------------------------------------------------------------------ --| Simple payroll calculation --| Author: Michael B. Feldman, The George Washington University --| Last Modified: July 1995 ------------------------------------------------------------------------ SUBTYPE NonNegFloat IS Float RANGE 0.0 .. Float'Last; Tax : CONSTANT NonNegFloat := 25.0; Hours : NonNegFloat; Rate : NonNegFloat; Gross : NonNegFloat; Net : NonNegFloat; BEGIN -- Weekly_Pay Ada.Text_IO.Put (Item => "Enter hours worked > "); Ada.Float_Text_IO.Get (Item => Hours); Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item => "Enter hourly rate > "); Ada.Float_Text_IO.Get (Item => Rate); Ada.Text_IO.New_Line; Gross := Hours * Rate; Net := Gross - Tax; Ada.Text_IO.Put (Item => "Gross pay is $ "); Ada.Float_Text_IO.Put (Item => Gross, Fore => 6, Aft => 2, Exp => 0); Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item => "Net pay is $ "); Ada.Float_Text_IO.Put (Item => Net, Fore => 6, Aft => 2, Exp => 0); Ada.Text_IO.New_Line; END Weekly_Pay;Sample Run
Enter hours worked > 40 Enter hourly rate > 6.50 Gross pay is $ 260.00 Net pay is $ 235.00
This program computes an employee's gross pay and net pay using the algebraic formulas
gross pay = hours worked x hourly rate
net pay = gross pay - tax amount
These formulas are written as the Ada assignment statements
Gross := Hours * Rate; Net := Gross - Tax;
in
the program. New values of Hours
and Rate
are read
each time the program is executed; a constant Tax
of $25.00 is
always deducted.
Near the top of the program, there is a line reading
SUBTYPE NonNegFloat IS Float RANGE 0.0 .. Float'Last;
The
purpose of this line, called a subtype declaration, is to indicate to
the compiler that certain variables and constants will have floating-point
values that must not be negative. The variables of the program are declared as
being of type NonNegFloat
because it does not make sense for an
employee's hours worked or hourly wage to be negative. We will return to this
question in
Section
2.10.
Program 2.7 first reads the data representing hours worked and hourly rate and then computes gross pay as their product. Next, it computes net pay by deducting a constant tax amount of 25.00. Finally, it displays the computed values of gross pay and net pay. Note in the program how the results are formatted.
In the sample run, note that we have entered 40
as an integer
token. Recall that this is allowed if the value is a whole number and that the
value is still stored internally as a float value.
Figure 2.6(a) shows the payroll program loaded into memory and the program memory area before execution of the program body.
Figure 2.6
The question mark in memory cells Hours
, Rate
,
Gross
, and Net
indicates that these variables are
undefined (value unknown) before program execution begins. During
program execution, the data values 40.0
and 4.50
are
read into the variables Hours
and Rate
, respectively.
After the assignment statements shown earlier are used to compute values for
Gross
and Net
, all variables are defined as shown in
Figure
2.6(b).
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.