-- This program does some simple analysis of the values found in an array.
-- It finds the sum of all of the values, and it
-- counts the number of positive, zero, and negative values
-- It displays the sum and the counts.
-- It also does a little very simple simple error checking.
-- For testing purposes, we fill up the array with some dummy values.
-- It would also be interesting to fill the array with random values,
-- but we'll skip that for now.
--
-- Author: Ned Okie
-- Date: 8/24/2009
with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
procedure countPosZeroNeg is
Num_Ints:
constant Natural := 10; -- Number of elements in the array
-- A type for our array
type Int_Array is array (1 .. Num_Ints) of Integer;
theNums: Int_Array; -- Array to hold the numbers
sum: Integer := 0; -- Sum of all values in the array
-- Number of positives, zeros, and negatives in the array
numPositives, numZeros, numNegatives: Natural := 0;
i: Natural; -- Counter for while loop
begin
-- Put some values into the array
theNums := (5, 0, -4, 9, 0, 3, -9, -77, 22, 11);
-- Sum all the elements of the array
for count in 1 .. Num_Ints loop
sum := sum + theNums(count);
end loop;
-- Now sum and count the positive and negative (and zero)
-- Use a separate loop just so we can illustrate the while loop
i := 1;
while i <= Num_Ints loop
-- Positives
if theNums(i) > 0 then
numPositives := numPositives + 1;
-- Zeros
elsif theNums(i) = 0 then
numZeros := numZeros + 1;
-- Negatives
else -- theNums(i) < 0 must be true
numNegatives := numNegatives + 1;
end if;
-- go to next array element
i := i + 1;
end loop;
-- Error Checking
if numPositives + numZeros + numNegatives /= Num_Ints then
put_line("ERROR IN COUNTS!!!");
end if;
-- Print sum and counts
put_line("Overall sum:" & sum'img);
put_line("Number of positives:" & numPositives'img);
put_line("Number of zeros:" & Natural'image(numZeros));
put_line("Number of Negatives:" & Integer'image(numNegatives));
end countPosZeroNeg;