Public classmatrixmultiply{ Public Static voidMain () {//declares a two-dimensional array of 2 rows and 3 columns to hold the matrix a int[,] Matrixa =New int[2,3]; //declares a two-dimensional array of 3 rows and 4 columns to hold the matrix B//Initialize Matrix B int[,] Matrixb =New int[3,4]{ {4,2,1,7}, {3,6,1,0}, {5,3,2,4} }; //declares a two-dimensional array of 2 rows and 4 columns that holds the product of matrix A and matrix B int[,] Matrixc =New int[2,4]; //Initialize matrix A for(inti =0; I <2; i++) { for(intj =0; J <3; J + +) {Matrixa[i,j]= (i +2) * (j +2) +1; } } //calculating the product of matrix A and matrix B for(inti =0; I <2; i++) { for(intj =0; J <4; J + +) { //Initialize Matrix CMATRIXC[I,J] =0; //computes the product of matrix A and matrix B, and stores the value in Matrix C for(intK =0; K <3; k++) {Matrixc[i,j]+ = matrixa[i,k] *Matrixb[k,j]; } } } //Print matrix AConsole.WriteLine ("\n******** Matrix a********"); Outputmatrix (Matrixa,2,3); //Print Matrix BConsole.WriteLine ("\n******** Matrix b********"); Outputmatrix (MATRIXB,3,4); //Print Matrix CConsole.WriteLine ("\n******** matrix A X matrix b********"); Outputmatrix (MATRIXC,2,4); } //function: Output matrix//Parameters://MatrixX: The Matrix to be printed//rowCount: Number of rows in a matrix//columnCount: Number of columns in a matrix Private Static voidOutputmatrix (int[,] MatrixX,intRowCount,intColumnCount) { //if the number of rows is wrong, the matrix is not printed if(Matrixx.length! = RowCount *ColumnCount) {Console.WriteLine ("The number of rows is wrong!"); return ; } //Print Matrix for(inti =0; i < RowCount; i++) { //Print all the elements in each row individually for(intj =0; J < ColumnCount; J + +) {Console.Write (Matrixx[i,j]+"\ t"); } //line BreakConsole.WriteLine (); } }}
Operation Result:
_12_ Matrix __matrixmultiply