Requirement
Compile a program to simulate Matrix Multiplication. Multiply the m * n matrix by an n * m matrix and output the result.
STEP 1 Calculation of Matrix Multiplication
A matrix of m rows and n columns can be multiplied by a matrix of n rows and p columns. The result is a matrix of m rows and p columns.
The number at the position of column j, row I of the product matrix, the sum of the n products obtained after the number of n in row I of the first matrix is multiplied by the number of n in column j of the second matrix.
STEP 2 programming ideas
(1) You need to apply for a dynamic two-dimensional array to store two matrices involved in calculation and one result matrix;
(2) manually enter two matrices involved in the operation;
(3) Call the function practice matrix multiplication calculation and output the result;
(4) release the memory occupied by dynamic arrays.
STEP 3 key points
(1) The Matrix involved in the operation is manually input data and the size of the two-dimensional array cannot be known in advance. Therefore, you need to apply for a dynamic array;
(2) because the m * n matrix and n * m matrix are not necessarily phalanx (that is, the number of rows and columns are equal), two parameters must be defined when applying for a dynamic array, control the length of rows and columns separately. Otherwise, an array out-of-bounds error is reported during running.
----------------------------------------- Lili split line ------------------------------------- the code is about to appear -----------------------------------
1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <string. h> 4 5 void product_matrix (int m, int n); 6 void dynamic_arr (int *** arr, int m, int n ); 7 void input_arr (int *** arr, int m, int n); 8 void free_arr (int *** arr, int x); 9 10 int main () 11 {12 int m, n; 13 14 printf ("Enter m, n: \ n"); 15 scanf ("% d, % d", & m, & n); 16 product_matrix (m, n); 17 18 return 0; 19} 20 21 void product_matrix (int m, int n) 22 {23 int I, x, y, sum; 24 int ** arr_a; 25 int ** arr_ B; 26 int ** arr_res; 27 28 input_arr (& arr_a, m, n); 29 input_arr (& arr_ B, n, m); 30 dynamic_arr (& arr_res, m, m); 31 32 printf ("the result of matrix multiplication is \ n"); 33 for (x = 0; x <m; x ++) 34 {35 for (y = 0; y <m; y ++) 36 {37 sum = 0; 38 for (I = 0; I <n; I ++) 39 {40 sum = sum + arr_a [x] [I] * arr_ B [I] [y]; 41} 42 arr_res [x] [y] = sum; 43 printf ("% 5d", arr_res [x] [y]); 44} 45 printf ("\ n"); 46} 47 48 free_arr (& arr_a, m); 49 free_arr (& arr_ B, n); 50 free_arr (& arr_res, m); 51} 52 53 void dynamic_arr (int *** arr, int m, int n) // apply for a dynamic array 54 {55 int I; 56 * arr = (int **) malloc (sizeof (int *) * m); 57 58 for (I = 0; I <m; I ++) 59 {60 (* arr) [I] = (int *) malloc (sizeof (int) * n); 61 memset (* arr) [I], 0, sizeof (int) * n); 62} 63} 64 65 void input_arr (int *** arr, int m, int n) // input array 66 {67 int I, j; 68 69 dynamic_arr (arr, m, n); 70 71 printf ("Enter % d integers: \ n", m * n ); 72 for (I = 0; I <m; I ++) 73 {74 for (j = 0; j <n; j ++) 75 scanf ("% d ", & (* arr) [I] [j]); 76} 77 78 printf ("output array: \ n"); 79 for (I = 0; I <m; I ++) 80 {81 for (j = 0; j <n; j ++) 82 printf ("% 5d", (* arr) [I] [j]); 83 printf ("\ n"); 84} 85 printf ("\ n "); 86} 87 88 void free_arr (int *** arr, int x) // release the occupied memory 89 {90 int I; 91 92 for (I = 0; I <x; I ++) 93 {94 free (* arr) [I]); 95 (* arr) [I] = 0; 96} 97 98 free (* arr ); 99 * arr = 0; 100}