Une solution de l'exercice 2 du TP n°7
program tp7exc2;
CONST nmax = 19;
VAR T : array[1..nmax,1..nmax] of integer;
n,i,j : integer;
scalaire,diagonale1,diagonale2, triangsup, trianginf,
quelconque : boolean;
choix : char;
BEGIN
repeat
(* saisir la taille du tableau *)
repeat
write('taille du tableau (1 à
19) ? ');
readln(n);
until (n>0) and (n<=nmax);
(* créer le tableau *)
randomize;
for i:=1 to n do for j:=1 to n do T[i,j]:=random(10);
(* afficher le tableau *)
for i:=1 to n do
begin
for j:=1 to n do write(T[i,j]:2);
writeln;
end;
(* préciser la répartition *)
(* triangulaire supérieure ? *)
triangsup:=true;
for i:=2 to n do for j:=1 to i-1 do
if T[i,j]<>0 then triangsup:=false;
(* triangulaire inférieure ? *)
trianginf:=true;
for i:=1 to n-1 do for j:=i+1 to n do
if T[i,j]<>0 then trianginf:=false;
(* diagonale 1 ? *)
diagonale1:=triangsup and trianginf;
(* diagonale 2 ? *)
diagonale2:=true;
for i:=1 to n do for j:=1 to n do
if ((i+j)<>n+4) and (T[i,j]<>0)
then diagonale2:=false;
(* scalaire ? *)
scalaire:=diagonale1;
for i:=1 to n do if T[i,i]<>T[1,1] then scalaire:=false;
(* quelconque ? *)
quelconque:=not(diagonale2 or triangsup or trianginf);
(* affichage *)
writeln;
writeln('Ce tableau est :');
if scalaire then writeln('scalaire');
if diagonale1 then writeln('diagonal 1') ;
if diagonale2 then writeln('diagonal 2');
if triangsup then writeln('triangulaire supérieur');
if trianginf then writeln('triangulaire inférieur');
if quelconque then writeln('quelconque');
(* continuer ou pas ... *)
writeln;
write('Un autre tableau (o/n) ? ') ;
readln(choix);
until upcase(choix)='N';
END.