ED n·4 Algorithmique et Programmation--------Corrigé |
Exercice 1 : Compaction d'un tableau
Question 2
Coder l'algorithme.
with Ada.Text_io;use Ada.Text_io;
with Ada.Integer_Text_io;use Ada.Integer_Text_io;
procedure compaction is
N:constant Natural:=15;
T:array(1..N) of Integer:=(4,0,0,3,0,0,7,0,0,0,1,0,3,0,0);
i:Natural:=0;
j:Natural:=N;
begin
while i<j loop
if T(j)=0
then
declare
x:Natural:=T(i+1);
begin
T(i+1):=T(j);
T(j):=x;
i:=i+1;
end;
else
j:=j-1;
end if;
end loop;
-- affichage du resultat
put("Tableau compacte : ");
for i in T'range loop
put(T(i),width=>2);
end loop;
new_line;
end compaction;