Design a matrix multiplication Program
Assume that
1 5 7 3 3 9 1 4 1 4
A = 3 6 3 9 B = 5 6 7 9 0 3
1 2 8 7 3 2 7 2 5 6
0 3 1 9 9 7 4 7 8 0
3 2 5 4
Returns the matrix of a * B.
Program conception:
The formula for matrix multiplication is as follows:
CIJ = the sum of the keys of AIK x bkj from 1 to n, you can use a three-layer loop to calculate this formula:
C () = a () * B () +) * B (4, 1)
= (1*3) + (5*5) + (7*3) + (3*9)
= 3 + 25 + 21 + 27
= 76
Likewise
C () = a () * B () +) * B (2, 2)
= (1*9) + (5*6) + (7*2) + (3*7)
= 9 + 30 + 14 + 21
= 74
In this way, we can obtain the Matrix Product of matrix A and matrix B.
Void main (void)
{
Int matrixa [5] [4] = {1, 5, 7, 3,
3,6, 3,9,
1, 2, 8, 7,
0, 3, 1, 9,
3, 2, 5, 4 };
Int matrixb [4] [6] =,
,
3, 2, 7, 2, 5, 6,
9, 7, 4, 7, 8, 0 };
Int matrixc [5] [6];
Int I, J, K;
For (I = 0; I <5; I ++)
For (j = 0; j <6; j ++)
{
Matrixc [I] [J] = 0;
For (k = 0; k <4; k ++)
Matrixc [I] [J] + = matrixa [I] [k] * matrixb [k] [J];
}
Printf ("the matrix A:/N ");
For (I = 0; I <5; I ++)
{
For (k = 0; k <4; k ++)
Printf ("% 5d", matrixa [I] [k]);
Printf ("/N ");
}
Printf ("/nthe matrixb:/N ");
For (k = 0; k <4; k ++)
{
For (j = 0; j <6; j ++)
Printf ("% 5d", matrixb [k] [J]);
Printf ("/N ");
}
Printf ("/nmatrix c = matrix A * matrix B/N ");
For (I = 0; I <5; I ++)
{
For (j = 0; j <6; j ++)
Printf ("% 5d", matrixc [I] [J]);
Printf ("/N ");
}
}
Running result:
The matrix:
1 5 7 3
3 6 3 9
1 2 8 7
0 3 1 9
3 2 5 4
The matrix B:
3 9 1 4 1 4
5 6 7 9 0 3
3 2 7 2 5 6
9 7 4 7 8 0
Matrix C = matrix A * matrix B:
76 74 97 84 60 61
129 132 102 135 90 48
100 86 99 87 97 58
99 83 64 92 77 15
70 77 68 68 60 48
Two-dimensional array row-column Interchange
Program instance:
Design a one-dimensional array that can convert a two-dimensional array into a column-based one-dimensional array and a row-based one-dimensional array.
The default two-dimensional array data is:
9 7 6 6
3 5 3 3
Data = 6 6 4 7
7 5 1 4
1 2 8 0
Program conception:
According to the meaning of the question, the data structure used in this program is "two-dimensional array", which is reported to convert the array to the primary column and to the row.
The size of the two-dimensional array is 5*4.
The array conversion formula based on rows is as follows:
Data [I] [J] position = (I * 4) + J
The column-based array conversion formula is as follows:
Data [I] [J] position = (J * 5) + I
Declare a 20-dimensional array to store data that is converted to a column for a long time, and declare a 20-dimensional array, it is used to store data that is dominated by rows after a long conversion.
Void main (void)
{
Int data [5] [4] = {9, 7, 6, 6,
3, 5, 3, 5,
6, 6, 4, 7,
7, 5, 1, 4,
1, 2, 8, 0 };
Int rawdata [20];
Int coldata [20];
Int I, J;
Printf ("the data of two dimen1_array:/N ");
For (I = 0; I <5; I ++)
{
For (j = 0; j <4; j ++)
Printf ("% 4D", data [I] [J]);
Printf ("/N ");
}
For (I = 0; I <5; I ++)
For (j =-; j <4; j ++)
Rowdata [I * 4 + J] = data [I] [J];
Printf ("/nthe row major matrix:/N ");
For (I = 0; I <20; I ++)
Printf ("% 3d", rowdata [I]);
Printf ("/N ");
For (I = 0; I <5; I ++)
For (j = 0; j <4; j ++)
Coldata [J * 5 + I] = data [I] [J];
Printf ("/nthe column major matrix:/N ");
For (I = 0; I <20; I ++)
Printf ("% 3d", coldata [I]);
Printf ("/N ");
}
Running result:
The data of two dimension array:
9 7 6 6
3 5 3 3
6 6 4 7
7 5 1 4
1 2 8 0
The row major matrix:
9 7 6 6 3 5 3 6 6 6 4 7 5 1 1 2 8 0
9 3 6 7 1 7 5 6 5 2 6 3 4 1 8 6 3 7 4 0
Exercise example:
Suppose there is a floating point two-dimensional array with 15 columns and 11 rows in total. The data storage method is Row-based, and the starting address in the memory is 30. Now, the address of the element in column 5th of the third row in the array is required.
Exercises:
You can understand the meaning of this question, and use row-based storage:
Data [I] [J] memory location = x + [(I * r) + J] * m
1) two-dimensional floating point array --> each element occupies 4 bytes, M = 4
2) The array size is 15 columns and 11 rows ---> r = 15
3) The starting address is 30 ---> X = 30
Data [3] [5] memory location = 30 + [(3*15) + 5] * 4 = 150
Exercise example:
Assume that the array demo is as follows:
3 9 1 4 1 4
5 6 7 9 0 3
Demo = 3 2 7 2 5 6
9 7 4 7 8 0
Find the relative position after the array is converted into a one-dimensional array with the primary column and row as the primary column.
Exercises:
1) dominated by rows:
Position in one-dimensional array |
0 |
1 |
2 |
3 |
... |
19 |
20 |
21 |
22 |
23 |
Location in the original array |
(0, 0) |
(0, 1) |
(0, 2) |
(0, 3) |
|
(3, 1) |
(3, 2) |
(3, 3) |
(3, 4) |
(3, 5) |
Content value |
3 |
9 |
1 |
4 |
... |
7 |
4 |
7 |
8 |
0 |
Dominated by columns:
Position in one-dimensional array |
0 |
1 |
2 |
3 |
... |
19 |
20 |
21 |
22 |
23 |
Location in the original array |
(0, 0) |
(1, 0) |
(2, 0) |
(3, 0) |
|
(3, 4) |
(0, 5) |
(1, 5) |
(2, 5) |
(3, 5) |
Content value |
3 |
5 |
3 |
9 |
... |
8 |
4 |
3 |
6 |
0 |