{ Matrix transponieren - Spiegeln an Diagonalen }
{ Autor: Dipl.-Ing. Thomas Speiser              }
{ Source from: http://Codes.TurboTools.de       }

program matrix1;
uses Crt;
const n = 5;
var matrix: array[1..n,1..n] of integer;
    x,y,i,j,temp,a: integer;

procedure Matrix_fuellen;
begin
randomize;
for i:=1 to n do
for j:=1 to n do begin matrix[i,j]:=random(9);
if i = j then Textcolor(red) else Textcolor(lightgray);
gotoxy(i,j);write(matrix[i,j]); end;
end;

begin
ClrScr;
Matrix_fuellen;

for i:=1 to n do
for j:=1 to n do begin
{Austauschen - Temp ist Zwischenwert}

{Kontrolle: Da nicht doppelt Vertauscht werden darf!!!}
if i-j < 0 then begin
temp:=matrix[i,j];
matrix[i,j]:=matrix[j,i];
matrix[j,i]:=temp; end;

{ [ 1  5  6]     [ 1  4  3]
  [ 4  2  0] ==> [ 5  2  1]
  [ 3  1  3]     [ 6  0  3] }

if i = j then Textcolor(red) else Textcolor(lightgray);
gotoxy(i+10,j);write(matrix[i,j]);
end;
readLN;
end.