Ada Generic Averaging Function

早过忘川 提交于 2019-12-10 17:28:17

问题


I have a function which averages a certain numeric value from an array of records. This value is either a natural or an enumerated type delta. I have it summing up the values correctly but my question is this: how do I get the length of an array into a generic type, so that it can divide both integers and delta type numbers?


回答1:


On your array-of-records use the 'Length attribute; this has the advantage of always working even if your bounds are somewhat odd, like -18..3, or an enumeration, like cheeses..fruits.

Something like:

Function Average( Input : In Array_of_Records ) Return float is
  -- You say you already have a summation function, so...
  Sum : Natural:= Summation( Input );
Begin
  Return Sum / Input'Length;
End Average;

You may need to convert the numeric types, by saying Float(Sum) or the like, as Ada does no automatic type "promotions."




回答2:


This has some flaws in it, but is this closer to what you wanted ?

NWS.

with Ada.Text_Io;

procedure Main is

   generic
      type Element_T is private;
      Zero : Element_T;
      One : Element_T;
      type Vec_T is array (Integer range <>) of Element_T;
      with function "+"(Left, Right : in Element_T) return Element_T is <>;
      with function "/"(Left, Right : in Element_T) return Element_T is <>;

   package Arrayops is
      function Sum (Vec : in Vec_T) return Element_T;
      function Count (Vec : in Vec_T) return Element_T;
      function Average (Vec : in Vec_T) return Element_T;
   end Arrayops;

   package body Arrayops is
      function Sum (Vec : in Vec_T) return Element_T is
         S : Element_T := Zero;
      begin
         for I in Vec'First .. Vec'Last loop
            S := S + Vec(I);
         end loop;
         return S;
      end Sum;

      function Count (Vec : in Vec_T) return Element_T is
         C : Element_T := Zero;
      begin
         for I in Vec'First .. Vec'Last loop
            C := C + One;
         end loop;
         return C;
      end Count;

      function Average (Vec : in Vec_T) return Element_T is
         S : constant Element_T := Sum (Vec);
         Len : constant Element_T := Count (Vec);
      begin
         return S / Len;
      end Average;
   end Arrayops;

   type Fl_Arr_T is array (Integer range <>) of Float;
   package Fl_Arr is new Arrayops (Element_T => Float,
                                   Zero => 0.0,
                                   One => 1.0,
                                   Vec_T => Fl_Arr_T);

   type Int_Arr_T is array (Integer range <>) of Integer;
   package Int_Arr is new Arrayops (Element_T => Integer,
                                    Zero => 0,
                                    One => 1,
                                    Vec_T => Int_Arr_T);


   My_Ints   : constant Int_Arr_T (1 .. 5) := (6,7,5,1,2);
   My_Floats : constant Fl_Arr_T (1 .. 7) := (6.1,7.2,5.3,1.4,2.5,8.7,9.7);

   Int_Sum   : constant Integer := Int_Arr.Sum (My_Ints);
   Int_Count : constant Integer := Int_Arr.Count (My_Ints);
   Int_Avg   : constant Integer := Int_Arr.Average (My_Ints);

   Float_Sum   : constant Float := Fl_Arr.Sum (My_Floats);
   Float_Count : constant Float := Fl_Arr.Count (My_Floats);
   Float_Avg   : constant Float := Fl_Arr.Average (My_Floats);

begin

   Ada.Text_Io.Put_Line ("Integers => Sum: " & Integer'Image (Int_Sum) & ", Count: " & Integer'Image (Int_Count) & ", Avg: " & Integer'Image (Int_Avg));
   Ada.Text_Io.Put_Line ("Floats   => Sum: " & Float'Image (Float_Sum) & ", Count: " & Float'Image (Float_Count) & ", Avg: " & Float'Image (Float_Avg));

end Main;

Result :

Integers => Sum: 21, Count: 5, Avg: 4

Floats => Sum: 4.09000E+01, Count: 7.00000E+00, Avg: 5.84286E+00




回答3:


Expanding on Shark8 a bit here...

Ada allows you to declare array types as unconstrained. Something like

type Array_of_Records is array (Natural range <>) of My_Record;

Gives you a type that can be used for arrays of records with starting and ending array indices that could be anywhere in the range of Natural.

One of the nifty things I can do with such a type is use it as a subroutine parameter, like so:

function Sum (Vector : in Array_of_Records) return Natural;

OK, so inside that routine, how do I know where the array bounds are? By using attributes, like so:

for index in Vector'first..Vector'last loop

or

for index in Vector'range loop

Of course for this to work, you must pass in a perfectly-sized array to your Sum routine. Supppose that isn't what you have. Suppose you instead have a huge array (kind of a buffer) and not all of the values are valid? Well, you keep track of what are the valid values, and pass in only those by using a slice.

Rec_Buffer : Array_of_Records (1..10_000);
Last_Valid_Rec : Natural := 0;
....
--// Rec_Buffer gets loaded with 2,128 values or something. We pass it into Sum
--// like so:
Ada.Text_IO ("Sum of vector is " & 
             natural'image(Sum (Rec_Buffer (1..Last_Valid_Rec));

(warning - uncompiled code)



来源:https://stackoverflow.com/questions/5213439/ada-generic-averaging-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!