.
..
exo01a.adb (download) (view)
exo02a.adb (download) (view)
exo03.adb (download) (view)
exo04.adb (download) (view)

-- put et get
with Ada.Text_Io, Ada.Integer_Text_Io;
use  Ada.Text_Io, Ada.Integer_Text_Io;

procedure Exo03 is
   -- declaration du tableau d'entiers
   N : constant Natural := 10;
   type T_TabNatural is array (1..N) of Natural;
   type T_MinMax is record
      Min : Natural;
      Max : Natural;
   end record;

   -- exo 3a
   procedure Get(T : out T_TabNatural) is
   begin
      for I in T'Range loop
         Get(T(I));
      end loop;
   end;

   -- exo 3bc
   procedure Get(T : out T_TabNatural; Nb : out Natural) is
      Val :  Natural;
   begin
      Nb := 0;
      -- version 1
      --        for I in T'Range loop
      --           Get(Val);
      --           exit when Val = 0;
      --           T(I) := Val;
      --           Nb := I;
      --        end loop;
      -- version 2
      loop
         Get(Val);
         exit when Val = 0;
         Nb := Nb + 1;
         T(Nb) := Val;
         exit when Nb = T'Last;
      end loop;
   end;

   -- exo 3d
   procedure Put(T : in T_TabNatural; K : in Natural := T_TabNatural'Last) is
   begin
      for I in 1..K loop
         Put(T(I));
      end loop;
   end;

   -- exo 3e
   function isIn(T : T_TabNatural;
                 X : Natural;
                 K : Natural := T_TabNatural'Last) return Boolean is
   begin
      for I in 1..K loop
         if T(I) = X then
            return True;
         end if;
      end loop;
      return False;
   end;

   -- exo 3f
   function countX(T : T_TabNatural;
                   X : Natural;
                   K : Natural := T_TabNatural'Last) return Natural is
      Nb : Natural := 0;
   begin
      for I in 1..K loop
         if T(I) = X then
            Nb := Nb + 1;
         end if;
      end loop;
      return Nb;
   end;

   -- exo 3g
   function Somme(T : T_TabNatural; K : Natural := T_TabNatural'Last)
                  return Natural is
      S : Natural := 0;
   begin
      for I in 1..K loop
         S := S + T(I);
      end loop;
      return S;
   end;

   -- exo 3h
   procedure MinMax(T : in T_TabNatural;
                    Min, Max : out Natural;
                    K : in Natural := T_TabNatural'Last) is
   begin
      if K < 1 then
         return;
      end if;
      Min := T(1);
      Max := T(1);
      for I in 1..K loop
         if T(I) > Max then
            Max := T(I);
         elsif T(I) < Min then
            Min := T(I);
         end if;
      end loop;
   end;

   -- exo 3i
   function MinMax(T : in T_TabNatural;
                   K : in Natural := T_TabNatural'Last) return T_MinMax is
      Res : T_MinMax := (0, 0);
   begin
      if K < 1 then
         return Res;
      end if;
      Res.Min := T(1);
      Res.Max := T(1);
      for I in 1..K loop
         if T(I) > Res.Max then
            Res.Max := T(I);
         end if;
         if T(I) < Res.Min then
            Res.Min := T(I);
         end if;
      end loop;

      return Res;
   end;

   Tbl : T_TabNatural;
   Nb : Natural;
   MM : T_MinMax;
   Min, Max : Natural;
begin
   -- version avec saisie du tableau entier
   Get(Tbl);

   Put("Affichage du tableau : ");
   Put(Tbl);
   New_Line;

   Put("Somme : ");
   Put(Somme(Tbl));
   New_Line;

   Put("Y a-t-il 1 dans le tableau : ");
   Put(Boolean'Image(IsIn(Tbl, 1)));
   New_Line;

   Put("Nombre de 1 : ");
   Put(Countx(Tbl, 1)); New_Line;

   -- appel de la fonction minmax
   MM := MinMax(Tbl);
   Put("(fct) min et max : ");
   Put(MM.Min); Put(MM.Max); New_Line;

   -- utilisation de la procedure minmax
   MinMax(Tbl, Min, Max);
   Put("(proc) min et max : ");
   Put(Min); Put(Max); New_Line;

   -- version avec saisie fini par 0
   Get(Tbl, Nb);
   Put("Nombre de donnees saisies : ");
   Put(Nb);
   New_Line;

   Put("Affichage du tableau : ");
   Put(Tbl, Nb);
   New_Line;

   Put("Somme : ");
   Put(Somme(Tbl, Nb));
   New_Line;

   Put("Y a-t-il 1 dans le tableau : ");
   Put(Boolean'Image(IsIn(Tbl, 1, Nb)));
   New_Line;

   Put("Nombre de 1 : ");
   Put(Countx(Tbl, 1, Nb));
end;
 
Webmaster : pierrefrancois.leon@laposte.net

Valid XHTML 1.0 Strict Valid CSS!