Previous topic | Ada Home Page | Index

Two dimensional arrays

----------------------------------------------------------- -- temperature.ada - process week of hourly temperatures -- -- Skansholm p 305-306 -- adapted by: Lawrie Brown / 29 Jul 94 ----------------------------------------------------------- with TEXT_IO; procedure temperature is type DAYS is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); -- days of week type HOURS is range 0..23; -- hours in a day type TEMPS is digits 3 range -99.9 .. 99.9; -- temps type MEASUREMENT_TABLE is array (DAYS, HOURS) of TEMPS; package DAY_IO is new TEXT_IO.ENUMERATION_IO (DAYS); package HOUR_IO is new TEXT_IO.INTEGER_IO (HOURS); package TEMP_IO is new TEXT_IO.FLOAT_IO (TEMPS); use TEXT_IO, DAY_IO, HOUR_IO, TEMP_IO; num_days : constant := 7; -- num days in week measurements : MEASUREMENT_TABLE; -- table of temps procedure Read_Temps(Tab : out MEASUREMENT_TABLE) is begin -- Read_Temps -- read values into table for d in DAYS loop PUT("Enter the temperature for "); PUT(d); NEW_LINE; -- get all values for one day for h in HOURS loop GET(Tab(d, h)); end loop; SKIP_LINE; end loop; end Read_Temps; procedure Write_Mean(M_tab : in MEASUREMENT_TABLE) is mean : TEMPS; begin -- Write_Mean -- write heading PUT_LINE("hr mean temp"); NEW_LINE; for h in HOURS loop -- average measurements for this hour mean := 0.0; for d in DAYS loop mean := mean + M_tab(d, h); end loop; -- compute mean mean := mean / TEMPS(num_days); -- display result PUT(h, width=>2); PUT(mean, exp=>0, fore=>7, aft=>1); NEW_LINE; end loop; end Write_Mean; begin -- temperature Read_Temps(measurements); Write_Mean(measurements); end temperature;


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