-- Programmer : Robert Sebesta
-- Modifier : Elizabeth Adams
-- CS 430/530
--
-- Environment: Windows 98 Second Edition
-- Compiler : GNAT
-- IDE : jGrasp
-- Language : Ada 95
-- Source File Name : Ada_Ex.adb
-- Executable : Ada_Ex.exe
--
-- Purpose: This program will determine how many of the data values entered by the
-- user are above the average of all of the data values entered by the user
-- Input: The number of data values to be input
-- The data values themselves
-- Output: A statement telling how many of the data values are greater than the average
--
-- Syntax Corrections made : Text in the book had typos
-- for loop uses for <lcv> in <start_value> .. <final_value> loop
-- multiple statements belonging to an if, must be delimited by begin and end
-- there should be an underscore not a period in Ada.Integer_Text_IO;
-- Modifications added
-- Data entered is echoed
-- Okay to have integer average since all data
is integer and we are
-- only interested
in which values are greater than the average not in the
-- average itself
-- Average is printed
-- Prompts for input
with Ada.Text_IO, Ada.Integer_Text_IO;
-- use Ada.Text_IO, Ada.Integer_Text_IO; -- I don't like use clauses
procedure Ada_Ex2 is
type Int_List_Type is array(1..99) of Integer; -- declares a new type which is a
-- 99 element array of integers whose first index is a 1
Int_List : Int_List_Type; -- uses the new type
List_Len, Sum,Average,Result:
Integer; --
-- all variables must be declared
begin -- marks the start of the executable code
Result := 0; -- := is the assignment operator
Sum := 0;
--
-- put_line is like Pascal's writln - after outputting quoted text it goes
-- to next line
Ada.text_io.put_line(" How many numbers do you want to enter? ");
Ada.Integer_text_io.get (List_Len); -- gets the integer number of items in list
if (List_Len > 0 ) and (List_Len < 100) then -- ada if statements require then
-- after condition, begin before
-- body and end if after body of
-- if statement and is a logical
-- operator
begin
Ada.text_io.put (" The number of numbers you plans to input is ");
Ada.Integer_Text_io.put (List_Len); -- output statement for an integer
Ada.text_io.new_line; -- put line doesn't work with integers
-- new line goes to a new line
-- new line can take a parameter representing the #
-- of lines to skip.
Ada.text_io.put_line (" Enter your numbers, 1 per line");
for Counter in 1..List_Len loop -- for loop uses .. where Pascal uses to
-- word loop is required at start of body
-- of loop. end loop is requited at end
-- of loop
Ada.Integer_text_io.Get (Int_List(Counter));
Sum := Sum + Int_List (Counter);
end loop; -- to get input
Average := Sum / List_Len; -- compute integer average
Ada.text_io.new_line;
Ada.Text_io.put_line (" Here are the integers you input ");
for Counter in 1.. List_Len loop -- checks each item against average
Ada.Integer_Text_io.put (Int_List(Counter));
if Int_List (Counter) > Average then
Result := Result + 1;
end if;
end loop;
Ada.text_io.new_line;
Ada.text_io.put (" The average is ");
Ada.Integer_Text_io.put ( Average);
Ada.text_io.new_line;
Ada.text_io.put (" The number of values > average is :");
Ada.Integer_text_io.put (Result);
Ada.text_io.New_Line;
end;
else
Ada.text_io.Put_Line (" Error-input list length is not legal");
end if;
end Ada_Ex2;