Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Although I know that your "problem" has been solved by the accepted answer, might I say that mucking around with pointers and array in C++ is looked down when you've right data structures in the standard library at your disposal without wasting additional resources or performace in using them. For a polynomial class I believe a map would do, and when you've holes i.e. 0 coefficient for a degree you won't be wasting space like you'd in your array implementation; additionally no worries allocating, freeing and leaking memory. I'd suggest you to atleast consider the below implementation once. I assure you that this can be further imrpoved a lot.</p> <pre><code>#include &lt;iostream&gt; #include &lt;map&gt; using namespace std; class Polynomial { public: int&amp; coefficient(unsigned degree) { return deg2coeff[degree]; } const int&amp; coefficient(unsigned degree) const { return deg2coeff.at(degree); } size_t size() const { return deg2coeff.size(); } Polynomial operator+(const Polynomial &amp;that) { Polynomial result; result.deg2coeff = (this-&gt;size() &gt; that.size()) ? deg2coeff : that.deg2coeff; const auto &amp;smaller = (this-&gt;size() &lt;= that.size()) ? *this : that; for (const auto &amp;it : smaller.deg2coeff) { result.coefficient(it.first) += smaller.coefficient(it.first); } return result; } friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; ostr, const Polynomial &amp;poly); private: map&lt;unsigned, int&gt; deg2coeff; }; std::ostream&amp; operator&lt;&lt;(std::ostream&amp; ostr, const Polynomial &amp;poly) { for (auto it = poly.deg2coeff.crbegin(); it != poly.deg2coeff.crend(); ++it) { if (it-&gt;second == -1) ostr &lt;&lt; '-'; else if (it-&gt;first == 0 || it-&gt;second != 1) { if (it-&gt;second &gt; 0) ostr &lt;&lt; "+"; ostr &lt;&lt; it-&gt;second; } if (it-&gt;first) { ostr &lt;&lt; "x"; if (it-&gt;first != 1) ostr &lt;&lt; "^" &lt;&lt; it-&gt;first; } } return ostr&lt;&lt;endl; } void read_polynomial(Polynomial &amp;poly) { cout &lt;&lt; "Number of degrees: "; unsigned n; cin &gt;&gt; n; int degree, coeff; for (unsigned i = 0; i &lt; n; ++i) { cout &lt;&lt; "Enter Degree, Coefficient: "; cin &gt;&gt; degree &gt;&gt; coeff; if (coeff) { poly.coefficient(degree) = coeff; } } } int main() { Polynomial p1; read_polynomial(p1); cout &lt;&lt; p1; Polynomial p2; read_polynomial(p2); cout &lt;&lt; p2; cout &lt;&lt; (p1 + p2); } </code></pre> <p><strong>Output</strong></p> <pre><code>Number of degrees: 4 Enter Degree, Coefficient: 9 2 Enter Degree, Coefficient: 3 -12 Enter Degree, Coefficient: 1 3 Enter Degree, Coefficient: 0 -27 +2x^9-12x^3+3x-27 Number of degrees: 2 Enter Degree, Coefficient: 13 10 Enter Degree, Coefficient: 11 34 +10x^13+34x^11 +10x^13+34x^11+2x^9-12x^3+3x-27 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload