The Ada Program: stats.adb

  1 -- stats.adb:  compute average, standard deviation of array of values
  2 
  3 with Ada.Float_Text_IO, Ada.Integer_Text_IO, Ada.Text_IO;
  4 with Ada.Numerics.Elementary_Functions;
  5 use Ada, Ada.Numerics.Elementary_Functions;
  6 
  7 procedure Stats is
  8 
  9    type Scores_Type is array (1..10) of Integer;
 10    Scores : Scores_Type := (39, 78, 51, 53, 81, 42, 69, 72, 79, 53);
 11    Sum: Integer;
 12    Average, Deviation: Float;
 13 
 14 begin
 15    -- Sum "Scores"
 16    Sum := 0;
 17    for I in Scores'Range loop
 18       Sum := Sum + Scores(I);
 19    end loop;
 20    Text_IO.Put ("Sum is ");
 21    Integer_Text_IO.Put (Sum, Width=>0);
 22    Text_IO.New_Line;
 23 
 24    Average := Float(Sum) / Float(Scores'Length);
 25    Text_IO.Put ("Average is ");
 26    Float_Text_IO.Put (Average, Aft=>2, Exp=>0);
 27    Text_IO.New_Line;
 28 
 29    Deviation := 0.0;
 30    for I in Scores'Range loop
 31       Deviation := Deviation + (Float(Scores(I))-Average)**2;
 32    end loop;
 33    Deviation := Sqrt (Deviation / (Float(Scores'Length-1)));
 34    Text_IO.Put ("Standard deviation is ");
 35    Float_Text_IO.Put (Deviation, Aft=>0, Exp=>0);
 36    Text_IO.New_Line;
 37 
 38    for I in Scores'Range loop
 39       Text_IO.Put ("Standardized score for ");
 40       Integer_Text_IO.Put (Scores(I), Width=>0);
 41       Text_IO.Put (" is ");
 42       Float_Text_IO.Put ((Float(Scores(I))-Average)/Deviation,
 43                          Aft=>0, Exp=>0);
 44       Text_IO.New_Line;
 45    end loop;
 46 
 47 end Stats;