Pascal

Résolu
guanaco750 Messages postés 15 Date d'inscription   Statut Membre Dernière intervention   -  
guanaco750 Messages postés 15 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour, je codais un programme dans lequel il y a une comparaison dans un vecteur et mon programme marche bien sauf cette comparaison. Alors j'ai refais un programme test avec juste une comparaison dans un vecteur et je ne comprend pas pour moi il devrait fonctionner. Regarder et dites moi ce qui cloche.
program teste;
type vect = array[1..10] of integer;
var a,b : vect;
    c : boolean;
    i,j,l : integer;

begin
     writeln('Entrez les elements');
     for i := 1 to 10 do
          begin
          write('Element ',i,' : ');
          readln(a[i]);
          end;
     j := 0 ;
     c := a[j] > a[j+1];
     for j := 1 to 10 do
         begin

         if a[j] > a[j+1] then b[j] := a[j];
         if a[j] < a[j+1] then b[j] := a[j+1];
         end;
     for l := 1 to 10 do
         begin
         writeln(b[l]);
         end;
     readln;
end.
A voir également:

2 réponses

domda91
 
j := 0 ;
J étant initialisé à 0,
c := a[j] > a[j+1];
est équivalent à:
c := a[0] > a[1];
a étant dimentionné à [1..10]
a[0] est hors limites

Pareil dans le code suivant lors de la dernière itération lorsque J vaut 10:

for j := 1 to 10 do
begin
if a[j] > a[j+1] then b[j] := a[j];
if a[j] < a[j+1] then b[j] := a[j+1];
end;
a[j+1] est alors équivalent à a[11] : hors limite

Enfin un élément de b peut rester non initialisé si a[j] = a[j+1]
0
guanaco750 Messages postés 15 Date d'inscription   Statut Membre Dernière intervention  
 
Je dois faire comment alors ? car si j'initialise j à 1, il y aura toujours le a[11].
0