Previous topic | Ada Home Page | Index

Loop example

Printing a table

Extend the original millionaire program to display a table of earnings.


----------------------------------------------------------
-- rich.adb - How long before I'm a millionaire?
-- Skansholm pp58-60
----------------------------------------------------------

with TEXT_IO; use TEXT_IO;

procedure rich is

    -- declare integer and float I/O libraries
    package int_io is new TEXT_IO.INTEGER_IO( INTEGER );
    package float_io is new TEXT_IO.FLOAT_IO( FLOAT );
    use  int_io, float_io;

    -- declare any constants and variables required
    number_of_days      : integer := 1;    -- days on job
    days_wage           : float := 0.01;   -- 1c today
    total_earnings      : float := 0.01;   -- on day 1
    desired_earnings    : float := 1000000.0;

----------------------------------------------------------
begin -- rich
    -- calculate earnings for each day till a millionaire
    PUT_LINE (" Day      Wage   Total Earnings");
    while total_earnings < desired_earnings loop
        number_of_days := number_of_days + 1;
        days_wage := days_wage * 2.0;  -- double pay :-)
        total_earnings := total_earnings + days_wage;
        PUT (number_of_days, width=>3);
        PUT (days_wage, fore=>8, aft=>2, exp=>0);
        PUT (total_earnings, fore=>8, aft=>2, exp=>0);
        NEW_LINE;
    end loop;
    PUT ("You will be a millionaire in ");
    PUT (number_of_days, width=>1);
    PUT_LINE (" days.");
end rich;

Program execution

 Day      Wage   Total Earnings
  2       0.02       0.03
  3       0.04       0.07
         . . . . .
 26  335544.31  671088.63
 27  671088.63 1342177.25
You will be a millionaire in 27 days.


Previous topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 14 Feb 96