Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to make a copy of a variable in c++
    primarykey
    data
    text
    <pre><code>#include &lt;iostream&gt; #include &lt;math.h&gt; using namespace std; class polynomial{ int degree; float *coefs; public: polynomial(int d); float operator()(float x); void operator==(polynomial P); polynomial operator^(int k); float operator[](float x); float *getcoefs(); }; polynomial::polynomial(int d){ int i; degree=d; if(d&gt;=0){ coefs=(float *)malloc((d+1)*sizeof(float)); cout&lt;&lt;"Create a polynomial of degree "&lt;&lt;d&lt;&lt;"\n"; for (i=d;i&gt;0;i--){ cout&lt;&lt;"The coefficient of x^"&lt;&lt;i&lt;&lt;" is : "; cin&gt;&gt;coefs[i]; cout&lt;&lt;"\n"; } cout&lt;&lt;"The constant is : "; cin&gt;&gt;coefs[0]; cout&lt;&lt;"\n"; cout&lt;&lt;"The polynomial is created\n"; } } </code></pre> <p>This is an overloading of the () operator so that P(x) returns the value of the polynomial:</p> <pre><code>float polynomial::operator()(float x){ float sum=0; int i,kstart; for(i=0;i&lt;=degree;i++){ if(coefs[i]!=0){ kstart=i; break; } } for(i=kstart;i&lt;=degree;i++){ sum+=coefs[i]*pow(x,i-kstart); } return sum; } </code></pre> <p>This is an overloading of the == that is copying the coefficients of one polynomial to another's:</p> <pre><code>void polynomial::operator==(polynomial P){ coefs=P.coefs; } </code></pre> <p>This is an overloading of the ^ so that P^k returns the k-th derivative of P:</p> <pre><code>polynomial polynomial::operator^(int k){ int i,j; polynomial G(-1); G.degree=this-&gt;degree; G==(*this); if(k&gt;G.degree){ for(i=0;i&lt;=G.degree;i++){ G.coefs[i]=0; } } else{ for(i=0;i&lt;k;i++){ G.coefs[i]=0; for(j=i+1;j&lt;=G.degree;j++){ G.coefs[j]*=(j-i); } } } return G; } float polynomial::operator[](float x){ return (x-(*this)(x)/(((*this)^1)(x))); } float *polynomial::getcoefs(){ return coefs; } int main(){ int i,d,maxiter,found; float seed,x1,x2,e; float *coefs; cout&lt;&lt;"The polynomial's degree is : "; cin&gt;&gt;d; cout&lt;&lt;"\n"; polynomial P(d),temp(-1); cout&lt;&lt;"-------Solve P(x)=0--------\n"; cout&lt;&lt;"Maxiterations = "; cin&gt;&gt;maxiter; cout&lt;&lt;"\n"; cout&lt;&lt;"Tolerance = "; cin&gt;&gt;e; cout&lt;&lt;"\n"; cout&lt;&lt;"Seed = "; cin&gt;&gt;seed; cout&lt;&lt;"\n"; found=0; x1=seed; for(i=0;i&lt;maxiter;i++){ memcpy((void *)(&amp;temp),(void *)(&amp;P),sizeof(polynomial)); x2=P[x1]; if(fabs(x2-x1)&lt;=e){ found=1; break; } coefs=temp.getcoefs(); cout&lt;&lt;coefs[0]&lt;&lt;"\n"; cout&lt;&lt;coefs[1]&lt;&lt;"\n"; cout&lt;&lt;coefs[2]&lt;&lt;"\n"; x1=x2; } if(found==1){ cout&lt;&lt;"A possible solution is x = "&lt;&lt;x2&lt;&lt;"\n"; } else{ cout&lt;&lt;"No solution found!\n"; } system("PAUSE"); return EXIT_SUCCESS; } </code></pre> <p>The problem can be seen in these testing lines:</p> <pre><code>cout&lt;&lt;coefs[0]&lt;&lt;"\n"; cout&lt;&lt;coefs[1]&lt;&lt;"\n"; cout&lt;&lt;coefs[2]&lt;&lt;"\n"; </code></pre> <p>The coefficients should stay the same,but now they change</p>
    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.
 

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