Une solution de l'exercice 3 du TP n°7

PROGRAM TP7EXC3;

CONST nmax = 20;

VAR A,B   : array[1..nmax,1..nmax] of integer;
    n,i,j : integer;
    choix : char;
    k   : integer;
 
BEGIN
randomize;
repeat
  (* saisir la taille des tableaux *)
  repeat
     write('taille des tableaux (1 à 19) ? ');
     readln(n);
     until (n>=1) and (n<=nmax);
 
  (* créer les tableaux A et B *)
  for i:=1 to n do for j:=1 to n do A[i,j]:=random(10);
  for i:=1 to n do for j:=1 to n do B[i,j]:=random(10);
  repeat
    (* menu et choix *)
    writeln;
    writeln('-1- afficher le tableau A');
    writeln('-2- afficher le tableau B');
    writeln('-3- afficher le tableau somme A+B');
    writeln('-4- afficher le tableau kA');
    writeln('-5- afficher le tableau transposé de B');
    writeln('-6- changer A et B');
    writeln('-0- terminer');
    repeat
      write('Votre choix ? ') ; readln(choix);
      until (choix>='0') and (choix<='6');

    (* réalisation du choix *)
    case choix of
     '1' : for i:=1 to n do
               begin
               for j:=1 to n do write(A[i,j]:2);
               writeln;
               end;
     '2' : for i:=1 to n do
               begin
               for j:=1 to n do write(B[i,j]:2);
               writeln;
               end;
     '3' : for i:=1 to n do
               begin
               for j:=1 to n do write(A[i,j]+B[i,j]:3);
               writeln;
               end;
     '4' : begin
           write('valeur de k (entier) ? ') ;readln(k);
           for i:=1 to n do
               begin
               for j:=1 to n do write(k*A[i,j]:5,' ');
               writeln;
               end;
           end;
     '5' : for i:=1 to n do
               begin
               for j:=1 to n do write(B[j,i]:2);
               writeln;
               end;
     end; { case }
  until (choix='0') or (choix='6');
until choix='0';
END.