How to get matrix size in Pascal

2 Answers

0 votes
program MatrixSize;

const
  Rows = 3;
  Columns = 4;

type
  TMatrix = array[1..Rows, 1..Columns] of Integer;

var
  Matrix: TMatrix;
  i, j, TotalCells: Integer;

begin
  // Initialize the matrix 
  Matrix[1,1] := 2; Matrix[1,2] := 0; Matrix[1,3] := 5; Matrix[1,4] := 9;
  Matrix[2,1] := 0; Matrix[2,2] := 4; Matrix[2,3] := 0; Matrix[2,4] := 3;
  Matrix[3,1] := 0; Matrix[3,2] := 8; Matrix[3,3] := 0; Matrix[3,4] := 1;

  // Calculate total cells 
  TotalCells := Rows * Columns;

  WriteLn('Matrix size: ', Rows, ' rows x ', Columns, ' columns');
  WriteLn('Total Cells: ', TotalCells);
end.



(*
run:

Matrix size: 3 rows x 4 columns
Total Cells: 12

*)

 



answered Oct 2 by avibootz
0 votes
program MatrixSize;

const
  MaxRows = 100;
  MaxCols = 100;

type
  TMatrix = array[1..MaxRows, 1..MaxCols] of Integer;

var
  Matrix: TMatrix;
  Rows, Columns, i, j, TotalCells, lenrows, lencols: Integer;

begin
  Rows := 3;
  Columns := 4;

  // Fill matrix with sample values 
  for i := 1 to Rows do
    for j := 1 to Columns do
      Matrix[i, j] := (i - 1) * Columns + (j - 1);
      
  lenrows := Length(matrix);
  lencols := Length(matrix[0]);

  // Calculate total cells 
  TotalCells := Rows * Columns;

  WriteLn('Matrix size: ', Rows, ' rows x ', Columns, ' columns');
  WriteLn('Matrix size: ', lenrows, ' rows x ', lencols, ' columns');
  WriteLn('Total Cells: ', TotalCells);

  // Print matrix 
  for i := 1 to Rows do
  begin
    for j := 1 to Columns do
      Write(Matrix[i, j]:4);
    WriteLn;
  end;
end.




(*
run:

Matrix size: 3 rows x 4 columns
Matrix size: 100 rows x 100 columns
Total Cells: 12
   0   1   2   3
   4   5   6   7
   8   9  10  11

*)

 



answered Oct 2 by avibootz
...