[Linear algebra] multiplication of Matrices
1 // Matrix Multiplication
2 # Include <iostream>
3 Using Namespace STD;
4
5
6 Int Main ()
7 {
8 Int Am,;
9 Int BM, Bn;
10 Int A [ 100 ] [ 100 ]; // Matrix
11 Int B [ 100 ] [ 100 ];// Matrix B
12 Int C [ 100 ] [ 100 ] = { 0 }; // Matrix C result matrix
13 Cout < " Enter the number of rows and columns of matrix. " ;
14 Cin> am>;
15 Cout < " Enter the number of rows and columns of matrix B. " ;
16 Cin> BM> Bn;
17 If (! = BM) // If the number of columns in matrix A must be different from that in matrix B, multiplication cannot be performed.
18 Cout < " The two matrices cannot be used for multiplication! " ;
19 Else
20 { // The number of columns in matrix A must be equal to the number of rows in matrix B before multiplication.
21 Cout < " Enter matrix " <Endl;
22 For (Int I = 0 ; I <am; I ++)
23 For ( Int J = 0 ; J <an; j ++)
24 {
25 Cin> A [I] [J];
26 }
27 Cout < " Enter matrix B " <Endl;
28 For ( Int I = 0 ; I <bm; I ++)
29 For ( Int J = 0 ; J <Bn; j ++)
30 {
31 Cin> B [I] [J];
32 }
33 // The following is a matrix multiplication calculation.
34 // The number of rows and columns in the result matrix is determined by matrix AB. The result matrix is C am * bn.
35 For ( Int I = 0 ; I <am; I ++)
36 For ( Int J = 0 ; J <Bn; j ++)
37 {
38 For ( Int II = 0 ; II <an; II ++)
39 {
40 C [I] [J] + = A [I] [II] * B [II] [J];
41 }
42 }
43 Cout < " The result of matrix A * matrix B is: " <Endl;
44 For ( Int I = 0 ; I <am; I ++)
45 {
46 For ( Int J = 0 ; J <Bn; j ++)
47 {
48 Cout <C [I] [J] < " " ;
49 }
50 Cout <Endl;
51 }
52 }
53 System ( " Pause " );
54 Return 0 ;
55 }