{"id":5327,"date":"2022-08-28T04:27:20","date_gmt":"2022-08-27T22:57:20","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/"},"modified":"2022-08-28T04:27:20","modified_gmt":"2022-08-27T22:57:20","slug":"solved-solving-linear-equations-via-adjunt-matrix-in-c-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/","title":{"rendered":"[Solved] Solving linear equations via adjunt matrix in C [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-48315608\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"48315608\" data-parentid=\"48296404\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The problem with getting uncommented code &#8220;<em>from a senior (buddy) of mine<\/em>&#8220;, is that you have absolutely no guidance and zero reference for the algorithms used in the code. Many times this leaves you in a worse position than you would be in if you had taken the time to learn the math involved and coded the solution yourself. <\/p>\n<p>Why? Instead of getting the benefit of learning the math involved and then logically applying that knowledge in forming a numeric solution, you end up guessing at where the error is in the code and needlessly guessing, recompiling, and failing again.<\/p>\n<p>Compounding that fact, the code you got from your buddy doesn&#8217;t even contain valid C. There is no &#8220;<code>For<\/code>&#8221; loop in C, there is no &#8220;<code>If<\/code>&#8221; statement and there is no &#8220;<code>Printf<\/code>&#8221; function. In fact none of the standard library functions and none of the operators start with a Capital Letter at all. The inclusion of the <code>curses.h<\/code> header is superfluous. <\/p>\n<p>Further, you invoke <em>Undefined Behavior<\/em> by allowing the value of <code>j<\/code> to exceed the bounds of your array <code>A<\/code> in the loop:<\/p>\n<pre><code>for(i=0;i&lt;3;i++){ \n   swap (&amp;A[i][j], &amp;A[j][i]);} \n<\/code><\/pre>\n<p>Why? Because the value of <code>j<\/code> is <code>3<\/code> following the nested loops where you fill <code>A<\/code> from <code>cof_x<\/code> and it is never reset prior to attempting the transpose.<\/p>\n<p>(<strong>note:<\/strong> that turns out to be only one of the problems with your attempt to transpose the cofactor matrix)<\/p>\n<p>One of you primary complaints is the code does not compile. Of course it doesn&#8217;t. However, if you <strong>enable compiler warnings<\/strong> the compiler would tell you the exact line where the problem occurs helping you fix the code so it does compile. For gcc, add <code>-Wall -Wextra -pedantic<\/code> to your compile string. For clang, add <code>-Weverything<\/code> and for VS (<code>cl.exe<\/code>) add <code>\/W3<\/code> (or <code>\/Wall<\/code> for literally all warnings). Do not accept code until it compiles cleanly without warning.<\/p>\n<p>Once you handle the basics, you can turn to the specifics of the algorithms. While the code forming the adjugate (or adjunct of squares) looked suspect, it in fact properly forms for adjugate matrix. The determinate is properly calculated. Where your code fails is computing the transpose the adjugate matrix by improperly swapping elements while iterating over the entire matrix.<\/p>\n<p>You have two options to transpose. Either iterate over the entire bounds <strong>assigning<\/strong> <code>A[j][i] = A[i][j];<\/code> or for a square matrix, iterate over only those elements that change swapping the values.<\/p>\n<p>After correcting the transpose (and commenting the code with reference to the operation at issue), the code works fine, e.g.<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n\n#define NDIM 3  \/* if you need a constant - define one (or more) *\/\n\nvoid swap (float *a, float *b)\n{\n    float temp;\n    temp = *a;\n    *a = *b;\n    *b = temp;\n}\n\nint main (void) {\n\n    int i, j, cof_1, cof_2, cof_3, cof_4;\n    float   a[NDIM][NDIM]   = {{0}},    \/* coefficient matrix *\/\n            c[NDIM]         =  {0},     \/* solution vector *\/\n            A[NDIM][NDIM]   = {{0}},    \/* adjunct matrix *\/\n            INV[NDIM][NDIM] = {{0}},    \/* inverse of a *\/\n            det             =  0.0,     \/* determinant of a *\/\n            X[NDIM]         =  {0},     \/* linear system roots *\/\n            B[NDIM]         =  {0};     \/* product A * c *\/\n\n    i = j = cof_1 = cof_2 = cof_3 = cof_4 = 0;  \/* initialize *\/\n\n    for (i = 0; i &lt; NDIM; i++) {    \/* validate input *\/\n        printf (\"Eq[%d] coefficients and constant: \", i + 1);\n        if (scanf (\"%f%f%f%f\", &amp;a[i][0], &amp;a[i][1], &amp;a[i][2], &amp;c[i]) != 4) {\n            fprintf (stderr, \"error: invalid coefficient.\\n\");\n            return 1;\n        }\n    }\n    putchar ('\\n');\n\n    \/* cofactor matrix from matrix of minors incorporating\n     * patterned +\/- sign to maxtix of minors.\n     *\/\n    for (i = 0; i &lt; NDIM; i++)\n        for (j = 0; j &lt; NDIM; j++) {\n            cof_1 = (i + 1) % NDIM;\n            cof_2 = (j + 1) % NDIM;\n            cof_3 = (i + 2) % NDIM;\n            cof_4 = (j + 2) % NDIM;\n\n            A[i][j]   = (a[cof_1][cof_2] * a[cof_3][cof_4]) -\n                        (a[cof_1][cof_4] * a[cof_3][cof_2]);\n        }\n\n    \/* compute determinant *\/\n    for (i = 0; i &lt; NDIM; i++)\n        det += a[i][0] * A[i][0];\n\n    \/* transpose cofactor (adjunct) *\/\n    for (i = 1; i &lt; NDIM; i++)\n        for (j = 0; j &lt; i; j++)\n            swap (&amp;A[i][j], &amp;A[j][i]);\n\n    \/* consistency check B *\/\n    for (i = 0; i &lt; NDIM; i++)\n        for (j = 0; j &lt; NDIM; j++)\n            B[i] += A[i][j] * c[j];\n\n    if (det == 0) { \/* eliminate infinite and no solution cases *\/\n        if (B[0] == 0 &amp;&amp; B[1] == 0 &amp;&amp; B[2] == 0) {\n            printf (\"The system is consistent and there \"\n                    \"are infinitely many solutions.\\n\");\n            return 1;\n        } else\n            printf (\"There is no solution to this system.\\n\");\n        return 2;\n    }\n\n    \/* compute inverse of A (1\/det * A) *\/\n    for (i = 0; i &lt; NDIM; i++)\n        for (j = 0; j &lt; NDIM; j++)\n            INV[i][j] = A[i][j] \/ det;\n\n    \/* multiply constant vector by inverse *\/\n    for (i = 0; i &lt; NDIM; i++)\n        for (j = 0; j &lt; NDIM; j++)\n            X[i] += INV[i][j] * c[j];\n\n    if (X[0] == 0 &amp;&amp; X[1] == 0 &amp;&amp; X[2] == 0)\n        \/* The case for trivial solution *\/\n        printf (\"This system has a trivial solution.\\n\");\n    else\n        \/* The case for unique solution *\/\n        printf (\"This system has a unique solution.\\n\\n\");\n\n    \/* output solutions for linear system of eq. *\/\n    for (i = 0; i &lt; NDIM; i++)\n        printf (\"\\t%c = %.2f\\n\", 'x' + i, X[i]);\n\n    return 0;\n}\n<\/code><\/pre>\n<p><strong>Example Use\/Output<\/strong><\/p>\n<p>Linear equations from a simple proof of a <code>30-60-90<\/code> triangle used for the example:<\/p>\n<pre><code>$ .\/bin\/matrixinv\nEq[1] coefficients and constant: 1 1 1 180\nEq[2] coefficients and constant: 5 -1 -1 0\nEq[3] coefficients and constant: 1 1 -1 0\n\nThis system has a unique solution.\n\n        x = 30.00\n        y = 60.00\n        z = 90.00\n<\/code><\/pre>\n<p>Look things over and let me know if you have further questions.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Solving linear equations via adjunt matrix in C [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The problem with getting uncommented code &#8220;from a senior (buddy) of mine&#8220;, is that you have absolutely no guidance and zero reference for the algorithms used in the code. Many times this leaves you in a worse position than you would be in if you had taken the time to learn the math involved &#8230; <a title=\"[Solved] Solving linear equations via adjunt matrix in C [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/\" aria-label=\"More on [Solved] Solving linear equations via adjunt matrix in C [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,1317],"class_list":["post-5327","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-linear-algebra"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Solving linear equations via adjunt matrix in C [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Solving linear equations via adjunt matrix in C [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The problem with getting uncommented code &#8220;from a senior (buddy) of mine&#8220;, is that you have absolutely no guidance and zero reference for the algorithms used in the code. Many times this leaves you in a worse position than you would be in if you had taken the time to learn the math involved ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-27T22:57:20+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Solving linear equations via adjunt matrix in C [closed]\",\"datePublished\":\"2022-08-27T22:57:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/\"},\"wordCount\":452,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"linear-algebra\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/\",\"name\":\"[Solved] Solving linear equations via adjunt matrix in C [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-27T22:57:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Solving linear equations via adjunt matrix in C [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Solving linear equations via adjunt matrix in C [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Solving linear equations via adjunt matrix in C [closed] - JassWeb","og_description":"[ad_1] The problem with getting uncommented code &#8220;from a senior (buddy) of mine&#8220;, is that you have absolutely no guidance and zero reference for the algorithms used in the code. Many times this leaves you in a worse position than you would be in if you had taken the time to learn the math involved ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/","og_site_name":"JassWeb","article_published_time":"2022-08-27T22:57:20+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Solving linear equations via adjunt matrix in C [closed]","datePublished":"2022-08-27T22:57:20+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/"},"wordCount":452,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","linear-algebra"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/","name":"[Solved] Solving linear equations via adjunt matrix in C [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-27T22:57:20+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-solving-linear-equations-via-adjunt-matrix-in-c-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Solving linear equations via adjunt matrix in C [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/5327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=5327"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/5327\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=5327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=5327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=5327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}