Algorithmique impérative/Écarts

Un livre de Wikilivres.
Algorithmique impérative
PyQt
PyQt
Sommaire
Théorie de l'algorithmique impérative
  1. Qu'est ce qu'un algorithme impératif Fait à environ 50 %
  2. Les types, les opérateurs et les expressions Fait à environ 50 %
  3. Les constantes, les variables Fait à environ 50 %
  4. Les instructions, les blocs d'instructions Fait à environ 50 %
  5. L'assignation Fait à environ 50 %
  6. Les exécutions conditionnelles Fait à environ 50 %
  7. Les structures itératives Fait à environ 50 %
  8. Les tableaux Fait à environ 50 %
  9. Les procédures et les fonctions Ébauche
  10. Le type enregistrement Fait à environ 50 %
  11. L'algorithme au final : vue d'ensemble En cours
  12. Exercices En cours
Outils de travail
Problèmes posés, analysés, résolus et commentés
Annexes
Modifier ce modèle ce sommaire

Problématique[modifier | modifier le wikicode]

Donner un algorithme qui, étant donné un tableau d'entiers, 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'entiers, trouve le plus grand écart entre deux éléments.

Solution[modifier | modifier le wikicode]

Une implémentation testable en Pascal[modifier | modifier le wikicode]

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.

Exemple de résultat produit par le programme :

[54|58|70|83|59|84] <- tableau donné
écart minimal trouvé : 83 - 84 = 1
écart maximal trouvé : 84 - 54 = 30