我有一個函數,它從記錄數組中平均某個數值。此值是自然類型或枚舉類型三角洲。我有它正確地總結了值,但我的問題是這樣的:我如何獲得一個數組的長度到一個泛型類型,以便它可以劃分整數和三角洲類型數字?Ada通用平均函數
4
A
回答
3
在您的數組記錄中使用'Length屬性;這具有始終工作的優勢,即使您的界限有點奇怪,如-18..3,或枚舉,如奶酪......水果。
喜歡的東西:
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;
您可能需要轉換的數值類型,說浮法(點心)等,作爲艾達不會進行自動型「的促銷活動。」
1
擴展在Shark8一點在這裏...
阿達允許你聲明數組類型爲無約束。像
type Array_of_Records is array (Natural range <>) of My_Record;
的東西給你,可用於與開始和結束數組的索引,可能是在Natural
範圍內的任何記錄陣列類型。
之一漂亮的東西,我可以用這樣的類型做的是把它作爲一個子程序參數,像這樣:
function Sum (Vector : in Array_of_Records) return Natural;
OK,使例程中,我怎麼知道數組邊界是?通過使用屬性,就像這樣:
for index in Vector'first..Vector'last loop
或
for index in Vector'range loop
當然這個工作,你必須通過一個完全大小的數組到您的總和程序。假設這不是你所擁有的。假設你有一個巨大的數組(緩衝區),並不是所有的值都是有效的?那麼,你跟蹤什麼是有效值,並通過使用切片僅通過那些。
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));
(警告 - 未編譯的代碼)
3
這有它的一些缺點,但是這是更接近你想要什麼?
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;
結果:
整數=>薩姆:21,計數:5,平均:4
浮標=>總:4.09000E + 01,次數:7.00000E + 00,平均: 5.84286E + 00
相關問題
- 1. 平均函數
- 2. 平均VBA函數
- 3. ddply:通過幾何平均函數
- 4. LINQ平均函數參數
- 5. 寫平均函數在Haskell
- 6. 圓平均Linq函數C#
- 7. 通過平均
- 8. 平均通過
- 9. R:通過平均
- 10. 計算平均次數的函數
- 11. Linq的平均值,參數函數
- 12. 函數平均在一個數組
- 13. 平均數平均精度蟒
- 14. 平均下來的平均數據列
- 15. 如何通過學校創建平均ses - tapply函數錯誤?
- 16. 通過按函數的加權平均值
- 17. 返回的平均函數nan
- 18. Excel下拉列表和平均函數
- 19. 移動平均函數問題
- 20. 執行1/R中的平均函數
- 21. Solr函數結果的平均值
- 22. 平均函數的標準誤差
- 23. Textfield作爲平均函數的輸入
- 24. 快速平均方差函數
- 25. SQL Server函數來計算平均
- 26. 加權平均減少函數在CrossFilter
- 27. SQL函數來計算平均
- 28. 在Python中獲取平均值函數
- 29. 爲平均函數進行SQL查詢
- 30. 相同的平均函數C++
這隻返回一個浮點數,我以爲海報要求的函數會返回一個浮點數或離散類型? – NWS 2011-03-07 17:33:56
正確;處理「或」的「返回浮動」部分。 – Shark8 2011-03-07 22:32:59
我解釋說或'它會根據泛型的實例返回這個或那個',因爲海報要求一個通用的:) – NWS 2011-03-09 10:29:51