Rem  Program matmul.bas
Rem  To multiply 2 matrices. The Maximum order of each matrix can be 40
Dim a(40,40), b(40,40), c(40,40)
MAXSIZE = 40
Rem Get dimensions of matrices
print "*** MATRIX - 1 ***"
gosub 1500
NrowA = Nrow : NcolA = Ncol
print "*** MATRIX - 2 ***"
gosub 1500
NrowB = Nrow : NcolB = Ncol

Rem Test whether the 2 matrices are compatible for multiplication
If NcolA <> NrowB Then
   print "Matrices are not compatible for multiplication"
   print "Program aborted"
   End
End If

REM GET THE MATRIX-A
PRINT "Enter the Matrix-1(One Element per line)"
FOR I = 1 TO NrowA
  PRINT USING "**********ROW = ##**********"; I
  FOR J = 1 TO NColA
    INPUT A(I,J)
  NEXT J
  PRINT
NEXT I

REM GET THE MATRIX-B
PRINT "Enter the Matrix-2(One Element per line)"
FOR I = 1 TO NrowB
  PRINT USING "**********ROW = ##**********"; I
  FOR J = 1 TO NColB
    INPUT B(I,J)
  NEXT J
  PRINT
NEXT I

Rem Multiply the two matrices
for i = 1 to NrowA
  for j = 1 to NcolB
     c(i,j) = 0
     for k = 1 to NcolA
        c(i,j) = c(i,j) + a(i,k) * b(k,j)
     Next k
  Next j
Next i

Rem Print the Result
print "*** MULTIPLIED MATRIX ***"
for i = 1 to NrowA
   PRINT USING "***********ROW =### ********"; i
   for j = 1 to NcolB
     PRINT USING "#####.##"; C(I,J)
   Next j
Next i
End
      
Rem Get the dimension of a matrix and validates the same
Rem NRow = No. of rows, NCol = No. of columns
1500:
Do
   Input "Enter No. of rows of Matrix"; Nrow
   If Nrow > 0 and Nrow <= MAXSIZE and Nrow = Int(Nrow) Then Exit Do
   print "Invalid Size"
Loop
Do
   Input "Enter No. of columns of Matrix"; Ncol
   If Ncol > 0 and Ncol <= MAXSIZE and Ncol = Int(Ncol) Then Exit Do
   print "Invalid Size"
Loop
Return

