Difficulty: ★ ★ ★ ☆
The task for last month’s Exercise was to describe complex data — a matrix. For this month’s Exercise, your task is to multiply these matrixes.
Matrix multiplication has practical applications that I’m not going to get into here. If you want to know more, I can recommend this video from Andy Math on YouTube. (He’s really worth a follow.)
Effectively, matrix multiplication involves manipulating rows and columns between two matrixes. The resulting matrix is based upon the dimensions of the two original matrixes, which makes sense when you understand why these manipulations are made. Again, watch the video for a practical example.
Consider the two matrixes shown in Figure 1, along with their product.
To calculate the results you use row and column positions across the two matrixes. From the figure, the first value calculated is 220, set in the first column, first row. To obtain this result you manipulate values from the first row in Matrix A and the first column in Matrix B, as illustrated in Figure 2.
For the result in the first row, second column, you perform the same action, but with the second column in Matrix B, which is illustrated in Figure 3.
The pattern repeats, marching down the rows of Matrix A and across the columns of Matrix B based upon the row/column position in the resulting matrix.
Your burden for this month’s Exercise is to write a C program that performs matrix multiplcation to generate the results shown in the Figures. Use the Matrix A and B examples. You can also base your code upon the solution provided for last month’s Exercise.
Here is output from my solution:
Matrix A
10 20 30
11 21 31
12 22 32
13 23 33
Matrix B
1 2
3 4
5 6
Matrix A * Matrix B
220 280
229 292
238 304
247 316
Please try this Exercise on your own before you peek at my solution.