The Ada Program: tpk.adb
1 -- tpk.adb: Knuth's TPK program in Ada95
2
3 with
4 Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO,
5 Ada.Numerics.Elementary_Functions;
6
7 use
8 Ada, Ada.Numerics.Elementary_Functions;
9
10 procedure TPK is
11
12 -- f(x) = sqrt(|x|) + 5*x**3
13 function f (x: Float) return Float is
14 begin
15 return (Sqrt (Abs x) + 5.0*x**3);
16 end f;
17
18 A: array (0 .. 10) of Float;
19
20 begin
21 -- Read in the values of the array "A"
22 for I in A'Range loop
23 Float_Text_IO.Get (A (I));
24 end loop;
25
26 -- In reverse order, apply "f" to each element of "A" and print
27 for I in reverse A'Range loop
28 declare
29 Y: constant Float := f (A (I));
30 begin
31 Integer_Text_IO.Put (I);
32 if (Y > 400.0) then
33 Text_IO.Put_Line (" too large");
34 else
35 Float_Text_IO.Put (Y);
36 Text_IO.New_Line;
37 end if;
38 end;
39 end loop;
40 end TPK;