C++ Graeffe's square root method

Discussion in 'Homework Help' started by klikaa, Jan 26, 2014.

  1. klikaa

    Thread Starter New Member

    Jan 26, 2014
    3
    0
    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


    Code ( (Unknown Language)):
    1.  
    Code ( (Unknown Language)):
    1.  
    2.  
    3. [LIST=1]
    4. [*]cout<<"How many elements?"<<endl;
    5. [*]    cin>>n;
    6. [*]    cout<<"Insert the elements:"<<endl;
    7. [*]    for(int i=0; i<n; i++)
    8. [*]    {
    9. [*]        cin>>elem[i];
    10. [*]
    11. [*]    }
    12. [*]
    13. [*]
    14. [*]    C[0]=pow(elem[0],2);
    15. [*]
    16. [*]    for(int j=1; j<n; j++)
    17. [*]    {
    18. [*]        C[j]=pow(elem[j],2);
    19. [*]
    20. [*]            int i=1;
    21. [*]            while((i+j)<=n-1&&(i<=j))
    22. [*]            {
    23. [*]
    24. [*]                C[j]=C[j]+(-1)^i*2*(elem[j-1]*elem[j+i]);
    25. [*]                i=i+1;
    26. [*]
    27. [*]            }
    28. [*]
    29. [*]
    30. [*]            cout<<"C"<<j<<":"<<C[j]<<endl;
    31. [*]
    32. [*]    }
    33. [/i][/LIST][i]
    34. [URL="http://latex.codecogs.com/gif.latex?A_%7Bk%7D=a_%7Bk%7D%5E%7B2%7D+2%5Csum_%7Bs=1%7D%5E%7Bk%7D%28-1%29%5E%7Bs%7Da_%7Bk-s%7D*a_%7Bk+s%7D,&space;k=0,....n"]
    35. [/URL]
    36. [URL="http://latex.codecogs.com/gif.latex?A_%7Bk%7D=a_%7Bk%7D%5E%7B2%7D+2%5Csum_%7Bs=1%7D%5E%7Bk%7D%28-1%29%5E%7Bs%7Da_%7Bk-s%7D*a_%7Bk+s%7D,&space;k=0,....n"][/URL][/i]
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,748
    4,796
    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.
     
  3. klikaa

    Thread Starter New Member

    Jan 26, 2014
    3
    0
    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
     
  4. box4831

    New Member

    Jan 26, 2014
    1
    0
    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
     
Loading...