C++ Graeffe's square root method

Thread Starter

klikaa

Joined Jan 26, 2014
3
So i have to write a c++ program for the Graeffe's square root method
I have am stuck here when i have this formula transform into c++ code, the formula is on the link
The code works particulary, the (elem[j-1]*elem[j+i]) doesn't work, it's beeing ignored and i don't know why... can any one help me?
http://latex.codecogs.com/gif.latex?A_{k}=a_{k}^{2}+2\sum_{s=1}^{k}(-1)^{s}a_{k-s}*a_{k+s},&space;k=0,....n


Rich (BB code):
Rich (BB code):
  1. cout<<"How many elements?"<<endl;
  2. cin>>n;
  3. cout<<"Insert the elements:"<<endl;
  4. for(int i=0; i<n; i++)
  5. {
  6. cin>>elem; [*] [*] } [*] [*] [*] C[0]=pow(elem[0],2); [*] [*] for(int j=1; j<n; j++) [*] { [*] C[j]=pow(elem[j],2); [*] [*] int i=1; [*] while((i+j)<=n-1&&(i<=j)) [*] { [*] [*] C[j]=C[j]+(-1)^i*2*(elem[j-1]*elem[j+i]); [*] i=i+1; [*] [*] } [*] [*] [*] cout<<"C"<<j<<":"<<C[j]<<endl; [*] [*] }
http://latex.codecogs.com/gif.latex...}^{k}(-1)^{s}a_{k-s}*a_{k+s},&space;k=0,....n
http://latex.codecogs.com/gif.latex...}^{k}(-1)^{s}a_{k-s}*a_{k+s},&space;k=0,....n
 

WBahn

Joined Mar 31, 2012
30,088
Please provide comments or descriptions for what the various parts of your code are supposed to do and what they are actually doing that is not what you expect.
 

Thread Starter

klikaa

Joined Jan 26, 2014
3
I had to write the formula above in c++, that what it supposed to do..
but it's ignoring one part of the formula and i don't know why
 

box4831

Joined Jan 26, 2014
1
One bit that is a problem is here: C[j]=C[j]+(-1)^i*2*(elem[j-1]*elem[j+i]);

The ^ doesn't raise a number to a power, it is a bitwise exclusive or operation. As it appears to be intended to switch back and forth from 1 and -1, you could put this expression in its place: ((i & 1) ? -1 : 1)

Also in this part of the expression: C[j]=C[j]+(-1)^i*2*(elem[j-1]*elem[j+i]); The latex formula you've given shows that it's multiplying Ak-s * Ak+s, however your expression appears to multiply Ak-1 * Ak+s (minus 1 instead of minus s, or 'i' in your code)

Try changing those two things and see if that works out.


Box
 
Top