program FlattenMatrix;
type
TCharRow = array[1..10] of char;
TMatrix = array[1..10] of TCharRow;
var
Matrix: TMatrix;
RowLengths: array[1..10] of integer;
NumRows: integer;
// Open array 'L' is 0-indexed
function FlattenMatrix(var M: TMatrix; var L: array of integer; Count: integer): string;
var
i, j: integer;
s: string;
begin
s := '';
// Open arrays in Pascal are always 0..High(L).
// If Count is 3, valid indices for L are 0, 1, and 2.
for i := 0 to Count - 1 do
for j := 1 to L[i] do
s := s + M[i + 1][j]; // Add 1 because M is a static array starting at 1
FlattenMatrix := s;
end;
var
Output: string;
begin
NumRows := 3;
{ Row 1: "Hello" }
RowLengths[1] := 5;
Matrix[1][1] := 'H'; Matrix[1][2] := 'e'; Matrix[1][3] := 'l'; Matrix[1][4] := 'l'; Matrix[1][5] := 'o';
{ Row 2: " World" }
RowLengths[2] := 6;
Matrix[2][1] := ' '; Matrix[2][2] := 'W'; Matrix[2][3] := 'o'; Matrix[2][4] := 'r'; Matrix[2][5] := 'l'; Matrix[2][6] := 'd';
{ Row 3: "!" }
RowLengths[3] := 1;
Matrix[3][1] := '!';
// Note: RowLengths is passed as an open array, so it becomes 0-indexed inside the function.
Output := FlattenMatrix(Matrix, RowLengths, NumRows);
writeln('Flattened string: ', Output);
end.
(*
run:
Flattened string: Hello World!
*)