1. ## 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!!!

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

3. I've been trying to present the formula as best I can.. it's no use... Anyway, sorry for the trouble... Thanks everyone...

4. 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. 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. Well in a loop, the way you get an alternating sign is something like this
Code:
int sign = 1;
for ( ... ) {
...
sign = -sign;
}

7. 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. 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. Come on, that code is just confusing. Put it all on the same level unless it's inside a new block:
Originally Posted by alyeska
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;

10. 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.

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.

11. 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. 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.

13. 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.

14. 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.

15. Originally Posted by iMalc
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.