Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,906 questions

51,838 answers

573 users

How to convert a character (flatten) matrix to a single string in Pascal

1 Answer

0 votes
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!

*)


 



answered Jan 10 by avibootz

Related questions

...