How to mirror a matrix across the main diagonal in Pascal

1 Answer

0 votes
program MirrorMatrix;

const
  SIZE = 3;

type
  MatrixType = array[1..SIZE, 1..SIZE] of Integer;

procedure PrintMatrix(matrix: MatrixType);
var
  i, j: Integer;
begin
  for i := 1 to SIZE do
  begin
    for j := 1 to SIZE do
      Write(matrix[i, j], ' ');
    Writeln;
  end;
end;

procedure MirrorMatrix(var matrix: MatrixType);
var
  i, j, temp: Integer;
begin
  for i := 1 to SIZE do
    for j := i + 1 to SIZE do
    begin
      temp := matrix[i, j];
      matrix[i, j] := matrix[j, i];
      matrix[j, i] := temp;
    end;
end;

var
  matrix: MatrixType;
begin
  matrix[1,1] := 1; matrix[1,2] := 2; matrix[1,3] := 3;
  matrix[2,1] := 4; matrix[2,2] := 5; matrix[2,3] := 6;
  matrix[3,1] := 7; matrix[3,2] := 8; matrix[3,3] := 9;

  Writeln('Original Matrix:');
  PrintMatrix(matrix);

  MirrorMatrix(matrix);

  Writeln('Mirrored Matrix:');
  PrintMatrix(matrix);
end.




(*
run:

Original Matrix:
1 2 3 
4 5 6 
7 8 9 
Mirrored Matrix:
1 4 7 
2 5 8 
3 6 9 

*)




 



answered Aug 28, 2025 by avibootz

Related questions

...