Transpose an arbitrarily sized rectangular Matrix.




$ include "seed7_05.s7i";
  include "float.s7i";
 
const type: matrix is array array float;
 
const func matrix: transpose (in matrix: aMatrix) is func
  result
    var matrix: transposedMatrix is matrix.value;
  local
    var integer: i is 0;
    var integer: j is 0;
  begin
    transposedMatrix := length(aMatrix[1]) times length(aMatrix) times 0.0;
    for i range 1 to length(aMatrix) do
      for j range 1 to length(aMatrix[1]) do
        transposedMatrix[j][i] := aMatrix[i][j];
      end for;
    end for;
  end func;
 
const proc: write (in matrix: aMatrix) is func
  local
    var integer: line is 0;
    var integer: column is 0;
  begin
    for line range 1 to length(aMatrix) do
      for column range 1 to length(aMatrix[line]) do
        write(" " <& aMatrix[line][column] digits 2);
      end for;
      writeln;
    end for;
  end func;
 
const matrix: testMatrix is [] (
    [] (0.0, 0.1, 0.2, 0.3),
    [] (0.4, 0.5, 0.6, 0.7),
    [] (0.8, 0.9, 1.0, 1.1));
 
const proc: main is func
  begin
    writeln("Before Transposition:");
    write(testMatrix);
    writeln;
    writeln("After Transposition:");
    write(transpose(testMatrix));
  end func;