Previous topic | Ada Home Page | Index

Unconstrained Arrays - Example

This example shows how a procedure can be written to add the elements of any array of integers.


-----------------------------------------------------------
--  sumarr.ada - sum array elements test program
--
--  written by: Lawrie Brown / 29 Jul 94
-----------------------------------------------------------

with TEXT_IO;

procedure sum_array is

    package int_io is new TEXT_IO.INTEGER_IO (INTEGER);
    use TEXT_IO, int_io;

    type int_u_array is array (INTEGER range <>) of INTEGER;

    small_int_array: int_u_array (1..3) :=(18, 15, 4);
    big_int_array: int_u_array (1..6) :=(1,4,9,16,25,36);
 
    --------------------------------------------------------
    -- function to sum the the items in an array
    function sum_iarr (in_arr:in int_u_array) 
                      return INTEGER is
        the_total : INTEGER := 0;      -- the running total
    begin -- sum_iarr
        for i in in_arr'RANGE loop
            the_total := the_total + in_arr(i);
        end loop;
        return the_total;
    end sum_iarr;
    --------------------------------------------------------

begin -- sum_array
    PUT("sum of small_int_array is ");
    PUT(sum_iarr(small_int_array), width=>4); NEW_LINE;

    PUT("sum of big_int_array is ");
    PUT(sum_iarr(big_int_array), width=>4); NEW_LINE;
end sum_array;


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