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

51,875 answers

573 users

How to multiply two matrices (matrix) in Pascal

1 Answer

0 votes
program MatrixMultiplication;

const
  RowsA = 2; // Number of rows in Matrix A
  ColsA = 3; // Number of columns in Matrix A
  RowsB = 3; // Number of rows in Matrix B
  ColsB = 2; // Number of columns in Matrix B

type
  Matrix = array of array of Integer;

var
  A, B, Result: Matrix;
  i, j, k: Integer;

begin
  // Initialize Matrix A
  SetLength(A, RowsA, ColsA);
  A[0][0] := 4; A[0][1] := 2; A[0][2] := 4;
  A[1][0] := 8; A[1][1] := 3; A[1][2] := 1;

  // Initialize Matrix B
  SetLength(B, RowsB, ColsB);
  B[0][0] := 3; B[0][1] := 5;
  B[1][0] := 2; B[1][1] := 8;
  B[2][0] := 7; B[2][1] := 9;

  // Initialize Result Matrix
  SetLength(Result, RowsA, ColsB);

  // Perform Matrix Multiplication
  for i := 0 to RowsA - 1 do
    for j := 0 to ColsB - 1 do
    begin
      Result[i][j] := 0;
      for k := 0 to ColsA - 1 do
        Result[i][j] := Result[i][j] + A[i][k] * B[k][j];
    end;

  // Display Result Matrix
  WriteLn('Result Matrix:');
  for i := 0 to RowsA - 1 do
  begin
    for j := 0 to ColsB - 1 do
      Write(Result[i][j]:4);
    WriteLn;
  end;
end.


 
(*
run:

Result Matrix:
  44  72
  37  73
 
*)

 



answered Jun 24, 2025 by avibootz
...