' Matrix Multiplication ' Written by Michael L. Jones, M. S., Spring 2002 ' Suffolk County Community College, Brentwood, New York ' Plan of attack: Input the dimensions of the matrices, A and B. The number ' of columns in A will equal the number of rows of B; otherwise, the ' multiplication cannot proceed. Perform the matrix multiplication to get C. ' Print A, B, and C. CLS INPUT "How many rows would you like for the first matrix"; arows INPUT "How many columns would you like for the first matrix"; acols brows = acols PRINT PRINT "There must be"; brows; "row(s) in the second matrix." INPUT "How many columns would you like for the second matrix"; bcols crows = arows ccols = bcols DIM A(arows, acols), B(brows, bcols), C(crows, ccols) ' Get matrix A PRINT FOR i = 1 TO arows FOR j = 1 TO acols PRINT "What is the value of A["; i; ","; j; "]"; INPUT A(i, j) NEXT j NEXT i ' Get matrix B PRINT FOR i = 1 TO brows FOR j = 1 TO bcols PRINT "What is the value of B["; i; ","; j; "]"; INPUT B(i, j) NEXT j NEXT i ' Calculate matrix C=A*B FOR i = 1 TO crows FOR j = 1 TO ccols C(i, j) = 0 FOR k = 1 TO acols ' Recall that the number of columns in A must equal the number of rows ' in B. Let k go from 1 to this number. To get the entry in row i, ' column j of C, multiply the first entry in row i of A by the first ' entry in column j of B, then the second entries, and so on. Add ' these products to find the value of C(i,j). C(i, j) = C(i, j) + A(i, k) * B(k, j) NEXT k NEXT j NEXT i ' Print matrix A PRINT PRINT "Matrix A:" PRINT FOR i = 1 TO arows FOR j = 1 TO acols PRINT A(i, j), NEXT j PRINT NEXT i ' Print matrix B PRINT PRINT "Matrix B:" PRINT FOR i = 1 TO brows FOR j = 1 TO bcols PRINT B(i, j), NEXT j PRINT NEXT i ' Print matrix C PRINT PRINT "Matrix C=A*B:" PRINT FOR i = 1 TO crows FOR j = 1 TO ccols PRINT C(i, j), NEXT j PRINT NEXT i