{"id":26252,"date":"2017-10-26T20:49:25","date_gmt":"2017-10-26T15:19:25","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26252"},"modified":"2017-10-26T20:49:25","modified_gmt":"2017-10-26T15:19:25","slug":"c-programming-program-add-two-polynomials","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-program-add-two-polynomials\/","title":{"rendered":"C++ Programming &#8211; Program to add two polynomials"},"content":{"rendered":"<p>Given two polynomials represented by two arrays, write a function that adds given two polynomials.<span id=\"more-142565\"><\/span><\/p>\n<p>Example:<\/p>\n<pre>Input:  A[] = {5, 0, 10, 6} \r\n        B[] = {1, 2, 4} \r\nOutput: sum[] = {5, 10, 30, 26, 52, 24}\r\n\r\nThe first input array represents \"5 + 0x^1 + 10x^2 + 6x^3\"\r\nThe second array represents \"1 + 2x^1 + 4x^2\" \r\nAnd Output is \"6 + 2x^1 + 14x^2 + 6x^3\"<\/pre>\n<p>Addition is simpler than <a href=\"http:\/\/www.geeksforgeeks.org\/multiply-two-polynomials-2\/\" target=\"_blank\" rel=\"noopener noreferrer\">multiplication of polynomials<\/a>. We initialize result as one of the two polynomials, then we traverse the other polynomial and add all terms to the result.<\/p>\n<pre><strong>add(A[0..m-1], B[0..n01])<\/strong>\r\n1) Create a sum array sum[] of size equal to maximum of 'm' and 'n'\r\n2) Copy A[] to sum[].\r\n3) Travers array B[] and do following for every element B[i]\r\n          sum[i] = sum[i] + B[i]\r\n4) Return sum[].<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p>The following is C++ implementation of above algorithm.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++ Program<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ Simple C++ program to add two polynomials<br\/>#include &lt;iostream&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ A utility function to return maximum of two integers<br\/>int max(int m, int n) {  return (m &gt; n)? m: n; }<br\/> <br\/>\/\/ A[] represents coefficients of first polynomial<br\/>\/\/ B[] represents coefficients of second polynomial<br\/>\/\/ m and n are sizes of A[] and B[] respectively<br\/>int *add(int A[], int B[], int m, int n)<br\/>{<br\/>   int size = max(m, n);<br\/>   int *sum = new int[size];<br\/> <br\/>   \/\/ Initialize the porduct polynomial<br\/>   for (int i = 0; i&lt;m; i++)<br\/>     sum[i] = A[i];<br\/> <br\/>   \/\/ Take ever term of first polynomial<br\/>   for (int i=0; i&lt;n; i++)<br\/>       sum[i] += B[i];<br\/> <br\/>   return sum;<br\/>}<br\/> <br\/>\/\/ A utility function to print a polynomial<br\/>void printPoly(int poly[], int n)<br\/>{<br\/>    for (int i=0; i&lt;n; i++)<br\/>    {<br\/>       cout &lt;&lt; poly[i];<br\/>       if (i != 0)<br\/>        cout &lt;&lt; &quot;x^&quot; &lt;&lt; i ;<br\/>       if (i != n-1)<br\/>       cout &lt;&lt; &quot; + &quot;;<br\/>    }<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    \/\/ The following array represents polynomial 5 + 10x^2 + 6x^3<br\/>    int A[] = {5, 0, 10, 6};<br\/> <br\/>    \/\/ The following array represents polynomial 1 + 2x + 4x^2<br\/>    int B[] = {1, 2, 4};<br\/>    int m = sizeof(A)\/sizeof(A[0]);<br\/>    int n = sizeof(B)\/sizeof(B[0]);<br\/> <br\/>    cout &lt;&lt; &quot;First polynomial is \\n&quot;;<br\/>    printPoly(A, m);<br\/>    cout &lt;&lt; &quot;\\nSecond polynomial is \\n&quot;;<br\/>    printPoly(B, n);<br\/> <br\/>    int *sum = add(A, B, m, n);<br\/>    int size = max(m, n);<br\/> <br\/>    cout &lt;&lt; &quot;\\nsumuct polynomial is \\n&quot;;<br\/>    printPoly(sum, size);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>First polynomial is\r\n5 + 0x^1 + 10x^2 + 6x^3\r\nSecond polynomial is\r\n1 + 2x^1 + 4x^2\r\nSum polynomial is\r\n6 + 2x^1 + 14x^2 + 6x^3<\/pre>\n<p><strong>Time complexity <\/strong>of the above algorithm and program is O(m+n) where m and n are orders of two given polynomials.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming &#8211; Program to add two polynomials &#8211; Mathematical Algorithms &#8211; Addition is simpler than multiplication of polynomials. We initialize result <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,83515,1,74058],"tags":[78159,78143,78167,77780,78170,77772,78166,70840,78162,78151,77751,78168,77754,78158,77630,78161,78153,78138,78149,73337,78150,78144,78142,78148,78155,78152,78137,78133,78141,78164,78136,78131,78135,78134,78154,77781,77016,78157,78146,78160,78169,78139,75430,78145,78156,78132,78165,78147,78140,78163],"class_list":["post-26252","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-3","category-coding","category-mathematical-algorithms","tag-0-1-2-3-4-5","tag-about-matlab","tag-add-polynomials","tag-adding-polynomials-calculator","tag-addition-c-program","tag-addition-of-polynomials","tag-addition-of-two-polynomials","tag-addition-program-in-c","tag-algorithm-for-polynomial-addition","tag-algorithm-for-polynomial-addition-using-array","tag-algorithm-for-polynomial-addition-using-linked-list-in-data-structure","tag-all-matlab","tag-c-addition-program","tag-c-program-addition","tag-c-program-for-addition","tag-c-program-for-addition-of-two-polynomials-using-linked-list","tag-c-program-to-multiply-two-polynomials-using-linked-list","tag-get-matlab","tag-how-to-add-polynomial","tag-linked-list-program-in-c","tag-list-of-polynomials","tag-matlab","tag-matlab-demo","tag-matlab-example-code","tag-matlab-find-function","tag-matlab-matrix-to-vector","tag-matlab-programming-basics","tag-matlab-programming-tutorial","tag-matlab-tutorial","tag-matlab-vector","tag-matrix-in-matlab","tag-matrix-matlab","tag-matrix-tutorial","tag-mbasic","tag-multiplying-polynomials-calculator","tag-polynomial-addition-algorithm-in-data-structure","tag-polynomial-addition-using-array-in-c","tag-polynomial-c-program","tag-polynomial-function","tag-polynomial-programming","tag-r-squared-matlab","tag-sanfoundry-c","tag-simple-addition-program-in-c","tag-simple-c-program-for-addition","tag-sin-4-5","tag-transpose-matrix-matlab","tag-vector-matlab","tag-vector-x","tag-what-is-matlab","tag-write-ac-program-to-add-two-numbers"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26252"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26252\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}