« Algorithmique impérative/Écarts » : différence entre les versions
Contenu supprimé Contenu ajouté
problèmatique : 3 exemples |
→Problèmatique : Une implémentation testable en Pascal |
||
Ligne 9 : | Ligne 9 : | ||
Donner un algorithme qui, étant donné un tableau d'entier, trouve le plus <u>grand</u> écart entre deux éléments. |
Donner un algorithme qui, étant donné un tableau d'entier, trouve le plus <u>grand</u> écart entre deux éléments. |
||
== Solution == |
|||
=== Une implémentation testable en Pascal === |
|||
<source lang="pascal" line> |
|||
program ecarts; |
|||
const |
|||
DEB = 0; |
|||
FIN = 10; |
|||
type |
|||
T_tabint = array [DEB..FIN] of integer; |
|||
procedure afficher(var t : T_tabint); |
|||
(* procédure qui affiche le tableau passé en paramètre *) |
|||
(* sur une ligne et sous la forme [a|b|c|d...|l|m] *) |
|||
var |
|||
i : integer; (* variable de boucle *) |
|||
begin |
|||
write('['); |
|||
for i := DEB to FIN-1 do write(t[i],'|'); |
|||
write (t[FIN],']'); |
|||
end; |
|||
procedure remplir(var t : T_tabint); |
|||
(* procédure qui remplit le tableau passé en paramètre *) |
|||
(* avec des nombres aléatoires pris entre 0 et 99 *) |
|||
var |
|||
i : integer; (* variable de boucle *) |
|||
begin |
|||
for i := DEB to FIN do t[i] := random(99); |
|||
end; |
|||
procedure RechercheEcartMin(t : T_tabint); |
|||
var |
|||
i,j : integer; (* variables de boucle *) |
|||
ind1,ind2 : integer; (* indices *) |
|||
ecart_min_trouve : integer; |
|||
begin |
|||
ecart_min_trouve := MAXINT; |
|||
for i := DEB to FIN-2 do begin |
|||
for j:= i+1 to FIN do begin |
|||
if (abs(t[i]-t[j]) <= ecart_min_trouve) then begin |
|||
ecart_min_trouve := abs(t[i]-t[j]); |
|||
ind1 := i; |
|||
ind2 := j; |
|||
end; |
|||
end; |
|||
end; |
|||
writeln('écart minimal trouvé : ',t[ind1],' - ',t[ind2],' = ',ecart_min_trouve) |
|||
end; |
|||
procedure RechercheEcartMax(t : T_tabint); |
|||
var |
|||
i : integer; (* variable de boucle *) |
|||
min,max : integer; (* indices du plus petit élément et du plus grand élément *) |
|||
begin |
|||
min := DEB; |
|||
max := DEB; |
|||
for i:= DEB to FIN do begin |
|||
if t[i] < t[min] then min := i; |
|||
if t[i] > t[max] then max := i; |
|||
end; |
|||
writeln('écart maximal trouvé : ',t[max],' - ',t[min],' = ',t[max]-t[min]) |
|||
end; |
|||
var |
|||
tab : T_tabint; |
|||
begin |
|||
remplir(tab); |
|||
afficher(tab); |
|||
writeln(' <- tableau donné'); |
|||
RechercheEcartMin(tab); |
|||
RechercheEcartMax(tab); |
|||
end. |
|||
</source> |
Version du 27 octobre 2007 à 13:58
Problèmatique
Donner un algorithme qui, étant donné un tableau d'entier, trouve le plus petit écart entre deux éléments.
Exemples :
- [1|10|4|6] : 6-4 = 2
- [1|10] : 10-1 = 9
- [5|5] : 5-5 = 0
Donner un algorithme qui, étant donné un tableau d'entier, trouve le plus grand écart entre deux éléments.
Solution
Une implémentation testable en Pascal
program ecarts;
const
DEB = 0;
FIN = 10;
type
T_tabint = array [DEB..FIN] of integer;
procedure afficher(var t : T_tabint);
(* procédure qui affiche le tableau passé en paramètre *)
(* sur une ligne et sous la forme [a|b|c|d...|l|m] *)
var
i : integer; (* variable de boucle *)
begin
write('[');
for i := DEB to FIN-1 do write(t[i],'|');
write (t[FIN],']');
end;
procedure remplir(var t : T_tabint);
(* procédure qui remplit le tableau passé en paramètre *)
(* avec des nombres aléatoires pris entre 0 et 99 *)
var
i : integer; (* variable de boucle *)
begin
for i := DEB to FIN do t[i] := random(99);
end;
procedure RechercheEcartMin(t : T_tabint);
var
i,j : integer; (* variables de boucle *)
ind1,ind2 : integer; (* indices *)
ecart_min_trouve : integer;
begin
ecart_min_trouve := MAXINT;
for i := DEB to FIN-2 do begin
for j:= i+1 to FIN do begin
if (abs(t[i]-t[j]) <= ecart_min_trouve) then begin
ecart_min_trouve := abs(t[i]-t[j]);
ind1 := i;
ind2 := j;
end;
end;
end;
writeln('écart minimal trouvé : ',t[ind1],' - ',t[ind2],' = ',ecart_min_trouve)
end;
procedure RechercheEcartMax(t : T_tabint);
var
i : integer; (* variable de boucle *)
min,max : integer; (* indices du plus petit élément et du plus grand élément *)
begin
min := DEB;
max := DEB;
for i:= DEB to FIN do begin
if t[i] < t[min] then min := i;
if t[i] > t[max] then max := i;
end;
writeln('écart maximal trouvé : ',t[max],' - ',t[min],' = ',t[max]-t[min])
end;
var
tab : T_tabint;
begin
remplir(tab);
afficher(tab);
writeln(' <- tableau donné');
RechercheEcartMin(tab);
RechercheEcartMax(tab);
end.