Description
Given two matrices A (N * t) and B (T * m), calculate c = a * B.
-
Input
-
The first line contains an integer T, indicating that there are T groups of test data.
The format of each group of test data is as follows:
The first three positive integers (n t m) represent the N * t of matrix A and the T * m of matrix B respectively, both smaller than or equal to 100.
The next n rows have t integers in each line and describe matrix;
Next we have t rows. Each row has m integers, which describe matrix B.
The value range is [-100,100].
-
Output
-
Output N * m row matrix C
For classic problems, follow the steps above in linear algebra.
# Include <stdio. h> # include <stdlib. h> // B [J] [k] * C [k] [I] = A [J] [I] void matrix (INT ** B, int ** C, int ** A, int NX, int NY, int NK) {int I, j, k; For (j = 0; j <NY; j ++) for (I = 0; I <NX; I ++) A [J] [I] = 0; For (j = 0; j <NY; j ++) {for (I = 0; I <NX; I ++) {for (k = 0; k <NK; k ++) A [J] [I] + = B [J] [k] * C [k] [I] ;};} void main () {int I, j, k, TMP; int B _row, B _col; int c_row, c_col; int a_row, a_col; int ** B, ** C, ** A; int number, Te; int N, t, M; scanf ("% d", & number); For (TE = 1; te <= number; Te ++) {scanf ("% d", & N, & T, & M); // enter the number of columns in array B, B _row = N; B _col = T; c_row = T; c_col = m; // enter the number of columns in the C array a_row = B _row; a_col = c_col; A = (INT **) malloc (sizeof (int *) * a_row ); for (j = 0; j <a_row; j ++) {A [J] = (int *) malloc (sizeof (INT) * a_col );} B = (INT **) malloc (sizeof (int *) * B _row); For (j = 0; j <B _row; j ++) {B [J] = (int *) malloc (sizeof (INT) * B _col);} c = (INT **) malloc (sizeof (int *) * c_row ); for (j = 0; j <c_row; j ++) {C [J] = (int *) malloc (sizeof (INT) * c_col );} // input B array element for (j = 0; j <B _row; j ++) for (I = 0; I <B _col; I ++) {scanf ("% d", & TMP); B [J] [I] = TMP;} // enter the C-array element for (j = 0; j <c_row; j ++) for (I = 0; I <c_col; I ++) {scanf ("% d", & TMP ); c [J] [I] = TMP;} matrix (B, C, A, a_col, a_row, B _col); For (j = 0; j <a_row; j ++) {for (I = 0; I <a_col; I ++) printf ("% d", a [J] [I]); printf ("\ n ");}}}