How to convert a specific row of a decimal matrix to a string in Pascal

2 Answers

0 votes
program ConvertRowToString;

const
  COLS = 6;

type
  TIntRow = array[1..COLS] of Integer;
  TIntMatrix = array[1..5] of TIntRow;

function ConvertRowToString(var matrix: TIntMatrix; row, cols: Integer): string;
var
  j: Integer;
  s, numStr: string;
begin
  s := '';

  for j := 1 to cols do
  begin
    Str(matrix[row][j], numStr);   { convert integer to string }
    s := s + numStr + ' ';
  end;

  ConvertRowToString := s;
end;

var
  matrix: TIntMatrix;
  row: Integer;
  resultStr: string;

begin
  matrix[1][1] := 4;   matrix[1][2] := 7;   matrix[1][3] := 9;
  matrix[1][4] := 18;  matrix[1][5] := 29;  matrix[1][6] := 0;
  
  matrix[2][1] := 1;   matrix[2][2] := 9;   matrix[2][3] := 18;
  matrix[2][4] := 99;  matrix[2][5] := 4;   matrix[2][6] := 3;
  
  matrix[3][1] := 9;   matrix[3][2] := 17;  matrix[3][3] := 89;
  matrix[3][4] := 2;   matrix[3][5] := 7;   matrix[3][6] := 5;
  
  matrix[4][1] := 19;  matrix[4][2] := 49;  matrix[4][3] := 6;
  matrix[4][4] := 1;   matrix[4][5] := 9;   matrix[4][6] := 8;
  
  matrix[5][1] := 29;  matrix[5][2] := 4;   matrix[5][3] := 7;
  matrix[5][4] := 9;   matrix[5][5] := 18;  matrix[5][6] := 6;

  row := 3;  { Turbo Pascal arrays start at 1, so row 2 in C++ is row 3 here }

  resultStr := ConvertRowToString(matrix, row, COLS);

  WriteLn(resultStr);
end.



(*
run:

9 17 89 2 7 5 

*)


 



answered Jan 9 by avibootz
0 votes
program ConvertRowToString;

const
  COLS = 6;

  matrix: array[1..5, 1..COLS] of Integer = (
    (4, 7, 9, 18, 29, 0),
    (1, 9, 18, 99, 4, 3),
    (9, 17, 89, 2, 7, 5),
    (19, 49, 6, 1, 9, 8),
    (29, 4, 7, 9, 18, 6)
  );


type
  TIntRow = array[1..COLS] of Integer;
  TIntMatrix = array[1..5] of TIntRow;

function ConvertRowToString(var matrix: TIntMatrix; row, cols: Integer): string;
var
  j: Integer;
  s, numStr: string;
begin
  s := '';

  for j := 1 to cols do
  begin
    Str(matrix[row][j], numStr);   { convert integer to string }
    s := s + numStr + ' ';
  end;

  ConvertRowToString := s;
end;

var
  row: Integer;
  resultStr: string;

begin

  row := 3;  { Turbo Pascal arrays start at 1, so row 2 in C++ is row 3 here }

  resultStr := ConvertRowToString(matrix, row, COLS);

  WriteLn(resultStr);
end.



(*
run:

9 17 89 2 7 5 

*)


 



answered Jan 9 by avibootz

Related questions

...