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,890 questions

51,817 answers

573 users

How to initialize a matrix with random characters in Pascal

1 Answer

0 votes
program RandomMatrix;

const
  ROWS = 3;
  COLS = 4;

type
  TMatrix = array[1..ROWS, 1..COLS] of char;

procedure PrintMatrix(var matrix: TMatrix);
var
  i, j: integer;
begin
  for i := 1 to ROWS do
  begin
    for j := 1 to COLS do
      write(matrix[i, j]:3);  { :3 gives setw(3)-like alignment }
    writeln;
  end;
end;

function GetRandomCharacter: char;
const
  characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
begin
  GetRandomCharacter := characters[1 + random(length(characters))];
end;

procedure InitializeMatrixWithRandomCharacters(var matrix: TMatrix);
var
  i, j: integer;
begin
  randomize;  { seed RNG with system clock }
  for i := 1 to ROWS do
    for j := 1 to COLS do
      matrix[i, j] := GetRandomCharacter;
end;

var
  matrix: TMatrix;

begin
  InitializeMatrixWithRandomCharacters(matrix);

  PrintMatrix(matrix);
end.



(*
run:

  K  k  1  X
  r  J  8  F
  3  3  r  G

*)

 



answered Nov 22, 2025 by avibootz
edited Nov 22, 2025 by avibootz
...