# Thread: alternating sum

1. Well at least with (i*i) you wont get function overheads, and its less work to type.

2. Hi all,
Thank you for all the helpful pointers. I couldn't get the sign=-sign idea to work, I guess, I have no idea how. Can somebody be more specific on how to do that?
Here's the code and the output though.. I still got warnings but it compiled anyway..

Code:
```#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double i = 0;
int sum = 0, n = 0;
int elements = 0;
cout << "Enter a value for n where n >= 1: ";
cin >> n;

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

Code:
```Enter a value for n where n >= 1: 4
1 + -4 + 9 + -16 = -10
Press any key to continue...```

3. Originally Posted by alyeska
Hi all,
Thank you for all the helpful pointers. I couldn't get the sign=-sign idea to work, I guess, I have no idea how. Can somebody be more specific on how to do that?
Well, that's pretty much it. The (-1)^i-1 goes back and forth between 1 and -1. So you start it off as 1; then after you use it, you set sign=-sign; and now it's -1. Then after you use it, you set sign=-sign; and now it's 1 again.

4. Originally Posted by Elysia
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.
Oh, I hadn't noticed that until you said today.
I guess it's only approx 10-20 times slower on recent versions of MSVC then. It's also O(log n) instead of O(1), as it takes longer the bigger the power is. Still no reason to use it when there are simpler ways.

5. Originally Posted by alyeska
Hi all,
Thank you for all the helpful pointers. I couldn't get the sign=-sign idea to work, I guess, I have no idea how. Can somebody be more specific on how to do that?
Well in your case you change sign to elements, but you basically write the line of code exactly as given.

The warnings you are getting are about conversion from double to int. The above would eliminate that.

6. Here is what I came up with. There is definitely a better way to write this but I will go for it this time. Thank you everyone, especially tabstop and iMalc.

Code:
```#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double i = 0;
int sum = 0, n = 0;
int elements = 0;
cout << "Enter a value for n where n >= 1: ";
cin >> n;

for (i = 1; i < n; ++i)
{
elements = (pow(-1.0,i-1))*(i * i);
sum += elements;
if (elements % 2 != 0)
cout << " + " << elements << " ";
else
cout << elements;
}
elements = (pow(-1.0,i-1))*(i * i);
sum += elements;
if (elements % 2 != 0)
cout << " + " << elements << " = " << sum << endl;
else
cout << elements << " = " << sum << endl;
}```
Code:
```Enter a value for n where n >= 1: 4
+ 1 -4 + 9 -16 = -10
Press any key to continue...```

Popular pages Recent additions