Previous topic |
Ada Home Page |
Index
Reverse FOR example
Specification
A program is required which will produce a conversion table from mph to
kph. The program will ask the user for the lowest and highest values to convert
between. The program will then display a conversion table starting with the
highest value and ending with the lowest value, the converted value being
displayed to 1 decimal place.
User interface
KPH TABLE
Please enter the lowest value 10
Please enter the highest value 20
20 mph is 32.3 kph
19 mph is 30.6 kph
18 mph is 29.0 kph
17 mph is 27.4 kph
16 mph is 25.8 kph
15 mph is 24.2 kph
14 mph is 22.5 kph
13 mph is 20.9 kph
12 mph is 19.3 kph
11 mph is 17.7 kph
10 mph is 16.1 kph
Algorithm
1. display table heading
2. get high and low values
2.1 prompt for and get low value
2.2 prompt for and get high value
3. do table
3.1 for speeds in mph from high to low
3.2 convert mph to kph
3.3 display mph and kph values
Data design
| NAME | TYPE | Notes |
| mph_to_kph | constant 1.61 | Conversion factor |
| high_value | INTEGER | Highest value in table |
| low_value | INTEGER | Lowest value in table |
| mph | INTEGER | Loop parameter |
| kph | FLOAT | kph equivalent of mph |
Program
-- Fintan Culwin Sept '88 v1.0
-- program to illustrate simple definite iteration
-- b1s5p2 see text pp63-64
with TEXT_IO;
package int_io is new TEXT_IO.INTEGER_IO( INTEGER );
with TEXT_IO;
package float_inout is new TEXT_IO.FLOAT_IO( float );
with TEXT_IO, int_io, float_inout;
use TEXT_IO, int_io, float_inout;
procedure b1s5p2 is
mph_to_kph : constant FLOAT := 1.61; -- conversion factor
low_val, -- lowest value
high_val : INTEGER; -- highest value
kph : FLOAT; -- kph equivalent
begin -- b1s5p2
-- show table heading
PUT_LINE( " KPH TABLE "); NEW_LINE;
-- get high and low
PUT( "Please enter the lowest value ");
GET( low_val ); SKIP_LINE;
PUT( "Please enter the highest value ");
GET( high_val); SKIP_LINE;
-- do table
for mph in reverse integer range low_val .. high_val loop
kph := FLOAT(mph) * mph_to_kph;
PUT( mph, width => 4); PUT(" is ");
PUT( kph, fore =>4, aft =>1, exp =>0);
PUT( " kph."); NEW_LINE;
end loop;
end b1s5p2;
Previous topic |
Ada Home Page |
Index
c-lokan@adfa.oz.au / 13 Feb 96