{"id":6591,"date":"2024-10-08T00:01:22","date_gmt":"2024-10-08T07:01:22","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6591"},"modified":"2024-10-05T10:57:00","modified_gmt":"2024-10-05T17:57:00","slug":"multiplying-matrixes-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6591","title":{"rendered":"Multiplying Matrixes &#8211; Solution"},"content":{"rendered":"<p>My solution for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6585\">this month&#8217;s Exercise<\/a> relies upon the solution from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6542\">last month&#8217;s Exercise<\/a>, specifically the way the matrixes are presented in the <em>main()<\/em> function and the use of the <em>output()<\/em> function. What I added is the <em>multiply()<\/em> function, which multiplies the two matrixes.<br \/>\n<!--more--><br \/>\nI confess that I had trouble working out my solution. I know that a nested loop is required to process values in both matrixes. But the solution requires another inner loop, which is the part that threw me.<\/p>\n<p>To be completely honest, my original solution included brute force code that processed each matrix cell individually. Only after seeing each step written out as its own statement did I code the innermost nested loop required to perform the calculations. Here is my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_10-Exercise.c\" rel=\"noopener\" target=\"_blank\">2024_10-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nstruct matrix {\r\n    int *grid;\r\n    int rows;\r\n    int columns;\r\n};\r\n\r\nvoid multiply(struct matrix a,struct matrix b)\r\n{\r\n    int r,c,x,v;\r\n\r\n    for( r=0; r&lt;a.rows; r++ )\r\n    {\r\n        for( c=0; c&lt;b.columns; c++ )\r\n        {\r\n            v = 0;\r\n            for( x=0; x&lt;a.columns; x++ )\r\n            {\r\n                v+= *(a.grid+x+r*a.columns) * *(b.grid+x*b.columns+c);\r\n            }\r\n            printf(\"%4d \",v);\r\n        }\r\n        putchar('\\n');\r\n    }\r\n}\r\n\r\nvoid output(char *title,struct matrix m)\r\n{\r\n    int x,y;\r\n\r\n    puts(title);\r\n    for( y=0; y&lt;m.rows; y++ )\r\n    {\r\n        for( x=0; x&lt;m.columns; x++ )\r\n        {\r\n            printf(\" %3d \",*(m.grid+x+y*m.columns) );\r\n        }\r\n        putchar('\\n');\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    int matrix_a[16] = {\r\n        10, 20, 30,\r\n        11, 21, 31,\r\n        12, 22, 32,\r\n        13, 23, 33\r\n    };\r\n    int matrix_b[6] = {\r\n        1, 2,\r\n        3, 4,\r\n        5, 6\r\n    };\r\n    struct matrix a = { matrix_a, 4, 3 };\r\n    struct matrix b = { matrix_b, 3, 2 };\r\n\r\n    <span class=\"comments\">\/* output each matrix *\/<\/span>\r\n    output(\"Matrix A\",a);\r\n    output(\"Matrix B\",b);\r\n\r\n    <span class=\"comments\">\/* multiple the matrixes *\/<\/span>\r\n    puts(\"Matrix A * Matrix B\");\r\n    multiply(a,b);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>multiply()<\/em> function contains a nested loop. It works through the rows of the first matrix (<code>a.rows<\/code>) and the columns of the second (<code>b.columns<\/code>).<\/p>\n<p>For the inner loop, variable <code>v<\/code> is initialized to zero. Then the innermost loop processes the rows and columns set from the outer loops. The values from the rows in matrix <code>a<\/code> (<code>r<\/code>) are multiplied by the values from the columns in matrix <code>b<\/code> (<code>c<\/code>). The total is accumulated in variable <code>v<\/code>:<\/p>\n<p><code>v+= *(a.grid+x+r*a.columns) * *(b.grid+x*b.columns+c);<\/code><\/p>\n<p>Pointer notation makes the expressions clunky, but the result obtains values from the rows and columns in each matrix. Also, the <code>**<\/code> thing is odd in the middle, which is why I added whitespace to separate the multiplication operator (<code>*<\/code>) from the pointer indirection operator (<code>*<\/code>). The order of operations maintains the sanity.<\/p>\n<p>Here is output:<\/p>\n<p><code>Matrix&nbsp;A<br \/>\n&nbsp;&nbsp;10&nbsp;&nbsp;&nbsp;20&nbsp;&nbsp;&nbsp;30&nbsp;<br \/>\n&nbsp;&nbsp;11&nbsp;&nbsp;&nbsp;21&nbsp;&nbsp;&nbsp;31&nbsp;<br \/>\n&nbsp;&nbsp;12&nbsp;&nbsp;&nbsp;22&nbsp;&nbsp;&nbsp;32&nbsp;<br \/>\n&nbsp;&nbsp;13&nbsp;&nbsp;&nbsp;23&nbsp;&nbsp;&nbsp;33&nbsp;<br \/>\nMatrix&nbsp;B<br \/>\n&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;4&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;5&nbsp;&nbsp;&nbsp;&nbsp;6&nbsp;<br \/>\nMatrix&nbsp;A&nbsp;*&nbsp;Matrix&nbsp;B<br \/>\n&nbsp;220&nbsp;&nbsp;280&nbsp;<br \/>\n&nbsp;229&nbsp;&nbsp;292&nbsp;<br \/>\n&nbsp;238&nbsp;&nbsp;304&nbsp;<br \/>\n&nbsp;247&nbsp;&nbsp;316&nbsp;<\/code><\/p>\n<p>Of course, the true test of my solution is whether it works on matrixes other than the two sample ones provided. I re-wrote the code to try the results on several different dimensioned matrixes and it worked! (Yes, I was relieved.)<\/p>\n<p>To confirm the results, you can use <a href=\"https:\/\/matrix.reshish.com\/multCalculation.php\" rel=\"noopener\" target=\"_blank\">this online matrix calculating tool<\/a> to verify the output.<\/p>\n<p>I hope that your solution met with success.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My solution for this month&#8217;s Exercise relies upon the solution from last month&#8217;s Exercise, specifically the way the matrixes are presented in the main() function and the use of the output() function. What I added is the multiply() function, which &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6591\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-6591","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6591"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6591\/revisions"}],"predecessor-version":[{"id":6619,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6591\/revisions\/6619"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}