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

51,811 answers

573 users

How to fill a matrix with 1 and 0 in random locations with Pascal

1 Answer

0 votes
program FillMatrixWith1And0InRandomLocationsProgram;

uses SysUtils;

const
  ROWS = 5;
  COLS = 4;

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

var
  matrix: TMatrix;

procedure FillMatrixWithRandom0And1(var matrix: TMatrix; rows, cols: Integer);
var
  i, j: Integer;
begin
  Randomize;
  for i := 1 to rows do
  begin
    for j := 1 to cols do
    begin
      matrix[i, j] := Random(2);  // Generates either 0 or 1
    end;
  end;
end;

procedure PrintMatrix(matrix: TMatrix; rows, cols: Integer);
var
  i, j: Integer;
begin
  for i := 1 to rows do
  begin
    for j := 1 to cols do
    begin
      Write(matrix[i, j], ' ');
    end;
    Writeln;
  end;
end;

begin
  FillMatrixWithRandom0And1(matrix, ROWS, COLS);
  
  PrintMatrix(matrix, ROWS, COLS);
end.




(*
run:

1 1 1 0 
0 0 0 0 
0 0 1 1 
1 0 0 0 
0 1 1 0 

*)

 



answered Jan 25, 2025 by avibootz

Related questions

1 answer 73 views
1 answer 83 views
1 answer 79 views
1 answer 66 views
1 answer 71 views
1 answer 64 views
...