-- This program illustrates using a length to specify how many elements
-- of an array have been read in and thus how many to process
-- Precondition: Input consists only of MaxSize or fewer integers, separated by whitespace
--
with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
procedure printsome is
MaxSize: Constant Integer := 10; -- Maximum size for the array
-- The array type has space for MaxSize elements
-- but some of them may not be used
type IntArray is array (1 .. MaxSize) of Integer;
-- Sums elements 1 .. count of array a
-- Precondition: c is in the range 1 .. MaxSize
function sumValues(a: IntArray; c: Natural) return Integer is
result: Integer := 0;
begin
for i in 1 .. c loop
result := result + a(i);
end loop;
return result;
end sumValues;
-- Prints elements 1 .. count of array a, and their squares
-- Precondition: c is in the range 1 .. MaxSize
procedure printValues(a: IntArray; c: Natural) is
begin
for i in 1 .. c loop
put(i);
put(a(i));
put(a(i) ** 2);
new_line;
end loop;
end printValues;
count: Natural := 0; -- The number of naturals that were read in
myArray: IntArray; -- Holds the naturals
begin
-- Fill the array by reading integers from standard input
while not end_of_file loop
count := count + 1;
get(myArray(count));
end loop;
-- Print the first count values in the array, and their squares
printValues(myArray, count);
-- Print the sum of the first count values in the array
put_line("Sum is " & Integer'image(sumValues(myArray, count)));
end printsome;