-- put et get
with Ada.Text_Io, Ada.Integer_Text_Io;
use Ada.Text_Io, Ada.Integer_Text_Io;
procedure Exo04 is
-- declaration du tableau d'entiers
N : constant Positive := 10;
type T_Tableau is array (1..N) of Integer;
-- exo 4a
procedure Get(T : out T_Tableau) is
begin
for I in T'Range loop
Get(T(I));
end loop;
end;
-- exo 4b
procedure Put(T : T_Tableau) is
begin
for I in T'Range loop
Put(T(I));
end loop;
end;
-- exo 4c
procedure Somme(T1, T2 : T_Tableau; T3 : out T_Tableau) is
begin
for I in T3'Range loop
T3(I) := T1(I) + T2(I);
end loop;
end;
-- exo 4d
procedure Compare(T1, T2 : T_Tableau; T3 : out T_Tableau) is
begin
for I in T3'Range loop
if T1(I) = T2(I) then
T3(I) := 0;
elsif T1(I) < T2(I) then
T3(I) := -1;
else
T3(I) := 1;
end if;
end loop;
end;
-- exo 4e
procedure Calc1(T : T_Tableau; S : out T_Tableau) is
begin
S(1) := T(1) + T(2);
for I in 2..N - 1 loop
S(I) := T(I - 1) + T(I) + T(I + 1);
end loop;
S(N) := T(N - 1) + T(N);
end;
-- exo 4f
procedure Calc2(T : T_Tableau; S : out T_Tableau) is
begin
for I in T'Range loop
S(I) := 0;
for J in I..N loop
S(I) := S(I) + T(J);
end loop;
end loop;
end;
-- exo 4g
procedure Calc3(T : T_Tableau; S : out T_Tableau) is
begin
for I in T'Range loop
S(I) := 0;
for J in 1..I loop
S(I) := S(I) + T(J);
end loop;
end loop;
end;
-- exo 4h = 4g
-- version 1
-- procedure Calc4(T : T_Tableau; S : out T_Tableau) is
-- Last : Integer := 0;
-- begin
-- for I in T'Range loop
-- S(I) := Last + T(I);
-- Last := S(I);
-- end loop;
-- end;
-- version 2
procedure Calc4(T : T_Tableau; S : out T_Tableau) is
begin
S(1) := T(1);
for I in 1..N loop
S(I) := S(I - 1) + T(I);
end loop;
end;
-- exo 4h = 4f
procedure Calc5(T : T_Tableau; S : out T_Tableau) is
Last : Integer := 0;
begin
for I in reverse T'Range loop
S(I) := Last + T(I);
Last := S(I);
end loop;
end;
Tbl1 : T_Tableau;
Tbl2 : T_Tableau;
Tbl3 : T_Tableau;
begin
Get(Tbl1);
Get(Tbl2);
Put_Line("Affichage des tableaux : ");
Put(Tbl1);
New_Line;
Put(Tbl2);
New_Line;
Put("Somme : ");
Somme(Tbl1, Tbl2, Tbl3);
Put(Tbl3);
New_Line;
Put("Compare : ");
Compare(Tbl1, Tbl2, Tbl3);
Put(Tbl3);
New_Line;
end;