Thread: alternating sum

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Question alternating sum

    Hi everyone..
    We were asked to write a program that inputs an integer n>=1, then compute and output the alternating sum with the formula:

    n
    ___
    \ j - 1 2 n - 1 2
    /__ ( -1) j = 1 - 4 + 9 - 16 + ... + (-1) n .
    j = 1

    For example: for n = 4, the output would be -10 because 1 - 4 + 9 - 16 = -10.

    I am not asking anybody to write me a program, I just need help understanding this formula. I don't get how the formula works. Substituting the values, I didn't get the same answer. I am probably in the wrong forum but I was just hoping somebody here knows ??

    Very sorry for the sloppy summation sign. I don't have the equation command installed in my editor..

    Thanks... any help will be great!!!
    Last edited by alyeska; 01-03-2008 at 09:28 PM. Reason: sloppy formula

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Code:
    n
    ___
    \               j - 1     2                                               n - 1     2
    /__    ( -1)           j       = 1 - 4 + 9 - 16 + ... + (-1)          n .
    j = 1
    Last edited by Salem; 01-04-2008 at 12:30 AM. Reason: code tags make it better, somewhat

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    I've been trying to present the formula as best I can.. it's no use... Anyway, sorry for the trouble... Thanks everyone...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    ASCII art really requires code tags. Are you trying for something like this?
    Code:
     n
    ----
    \         j-1 2
     \    (-1)   j
     /
    /
    ----
    j=1
    Anyway, all the formula says is this: for each number between 1 and n, put it in the formula off to the right for j. Then take all those numbers and add them up.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Yes, that's what I was trying to present.
    Code:
                 n
                sum  (-1)^j-1  j^2 = 1 - 4 + 9 - 16 + ... + (-1)^n-1  n^2.
                j=1
    There, that's more like it..

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well in a loop, the way you get an alternating sign is something like this
    Code:
    int sign = 1;
    for ( ... ) {
        ...
        sign = -sign;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Hi again...
    This code is what I have so far. I must be using the pow(x,y) function wrong. I have been getting these errors. Is this the correct way to write the formula in code? Or do I have to manually declare to alternate the sign?

    Code:
    for (i = 1; i < n; ++i)
    	{
    		sum = (pow(-1,n-1))(pow(n,2));
    		cout << i << " + ";
    	}
    	sum = (pow(-1,n-1))(pow(n,2));
    	cout << i << " = " << sum << endl;


    Code:
    error C2668: 'pow' : ambiguous call to overloaded function
    1>        c:\program files\microsoft visual studio 8\vc\include\math.h(575): could be 'long double pow(long double,int)'
    1>        c:\program files\microsoft visual studio 8\vc\include\math.h(527): or 'float pow(float,int)'
    1>        c:\program files\microsoft visual studio 8\vc\include\math.h(489): or 'double pow(double,int)'
    1>        while trying to match the argument list '(int, int)'
    Thanks for all the help..

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to explicitly put a * between the two calls to pow.

    Why are you always outputting i?

    You shouldn't need an extra call outside of the for-loop; make the second condition i <= n instead.

    You call the variable "sum", but you never add anything.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Come on, that code is just confusing. Put it all on the same level unless it's inside a new block:
    Quote Originally Posted by alyeska View Post
    Code:
    for (i = 1; i < n; ++i)
    {
    	sum = (pow(-1,n-1))(pow(n,2));
    	cout << i << " + ";
    }
    sum = (pow(-1,n-1))(pow(n,2));
    cout << i << " = " << sum << endl;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Smile

    You need to explicitly put a * between the two calls to pow.

    Why are you always outputting i?

    You shouldn't need an extra call outside of the for-loop; make the second condition i <= n instead.

    You call the variable "sum", but you never add anything.

    Thanks for replying..
    I modified the code, put the * sign and added a variable. You were right, I didn't add anything for the sum.
    I am trying to output all the elements (i) and I did the extra call to cout i because I didn't want the cout << "+" on the last element but the sum instead.
    I am a beginner and I am fully aware that my codes are not as efficient.
    Thanks for your help..

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Forgot to post the code.. sorry.. I am still getting these errors..

    Code:
    for (i = 1; i < n; ++i)
    	{
    		elements = (pow(-1,i-1))*(pow(i,2));
    		sum += elements;
    		cout << i << " + ";
    	}
    	elements = (pow(-1,i-1))*(pow(i,2));
    	sum += elements;
    	cout << i << " = " << sum << endl;

    Code:
    error C2668: 'pow' : ambiguous call to overloaded function
    1>        c:\program files\microsoft visual studio 8\vc\include\math.h(575): could be 'long double pow(long double,int)'
    1>        c:\program files\microsoft visual studio 8\vc\include\math.h(527): or 'float pow(float,int)'
    1>        c:\program files\microsoft visual studio 8\vc\include\math.h(489): or 'double pow(double,int)'
    1>        while trying to match the argument list '(int, int)'

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Inside pow, use -1.0 instead of -1 (it will be treated then as a double). Is elements defined as an integer? If so, you will get a message that a double is being coerced into an int. I forget right now whether that's a warning or an error; in this context, you can cast back to an integer if you need to.

    Edit: Or better yet, you can use the sign=-sign trick mentioned above and i*i for pow(i,2) and then everything will still be an integer. That would be best.

    You're still printing out i instead of elements.
    Last edited by tabstop; 01-04-2008 at 06:09 PM. Reason: brainwave!

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Any implicit conversion between floating and integer type is an error IIRC. In any case, if it isn't an error, then you'll get a warning, so the best is to do an explicit cast.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't use pow! That would make it quite literally about 100 times slower.
    if you have i and you want to square it, just use a = (i*i).
    No it is not premature optimisation to use a simple multiplication here, it's intentional deoptimisation to use pow!

    Using a power of -1 is also just silly. They use -1 to a power in formulas because it's an easy way of mathematically writing "multiplied by -1 if n is odd".
    But in a real program, you would simply use negation: a = -a if n was odd.

    You only have to add up the (possibly negated) squares in a simple for-loop and you're done. It's an extremely easy problem. You're just scared off by the maths formula.
    Last edited by iMalc; 01-04-2008 at 06:33 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by iMalc View Post
    Don't use pow! That would make it quite literally about 100 times slower.
    I would say that depends.
    In Microsoft's implementation, at least, when you do power to an integer, it does a simple multiplication loop. Though if you do power against a double, it will enter complicated assembly code with lots of instructions to get the result.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Application Repeatition
    By lgcarter in forum C Programming
    Replies: 3
    Last Post: 06-16-2009, 02:07 PM
  2. Replies: 1
    Last Post: 05-28-2009, 01:28 PM
  3. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  4. a sum equal to or in excess of 100
    By lyoncourt in forum C Programming
    Replies: 6
    Last Post: 10-07-2007, 05:43 PM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM