3

I wrote the following code to sum the series (-1)^i*(i/(i+1)). But when I run it I get -1 for any value of n.

Can some one please point out what I am doing wrong? Thank you in advance!

#include <iostream>
using namespace std;

int main()
{
    int sum = 0;
    int i = 1.0;
    int n = 5.0;

    for(i=1;i<=n;i++)
        sum = (-1)^i*(i/(i+1));

    cout << "Sum" <<" = "<< sum << endl;
    return 0;
}
Mysticial
  • 464,885
  • 45
  • 335
  • 332
Stripers247
  • 2,265
  • 11
  • 38
  • 40
  • 3
    What do you think the `^` operator does? Are you trying to perform an exclusive-or operation, or are you trying to perform exponentiation? – Robᵩ Feb 03 '12 at 20:18
  • 1
    And why do you assign a floating point to an integer? (`int i = 1.0`) – Yakov Galka Feb 03 '12 at 20:19
  • well first your setting sum every single pass of the loop and not adding to the previous value of it. second ^ does not raise a power like you would think. check out the c math library. and watch out for implicit casts that happen when you divide into a fraction. – L7ColWinters Feb 03 '12 at 20:22

7 Answers7

14

Problem #1: The C++ ^ operator isn't the math power operator. It's a bitwise XOR.

You should use pow() instead.

Problem #2: You are storing floating-point types into an integer type. So the following will result in integer division (truncated division):

i/(i+1)

Problem #3: You are not actually summing anything up:

sum = ...

should be:

sum += ...

A corrected version of the code is as follows:

double sum = 0;
int i = 1;
int n = 5;

for(i = 1; i <= n; i++)
    sum += pow(-1.,(double)i) * ((double)i / (i + 1));

Although you really don't need to use pow in this case. A simple test for odd/even will do.

double sum = 0;
int i = 1;
int n = 5;

for(i = 1; i <= n; i++){
    double val = (double)i / (i + 1);
    if (i % 2 != 0){
        val *= -1.;
    }
    sum += val;
}
Mysticial
  • 464,885
  • 45
  • 335
  • 332
4

You need too put sum += pow(-1,i)*(i/(i+1));

Otherwise you lose previous result each time.

Use pow function for pow operation.

edit : as said in other post, use double or float instead of int to avoid truncated division.

xpda
  • 15,585
  • 8
  • 51
  • 82
ClemPi
  • 316
  • 1
  • 4
3

How about this

((i % 2) == 0 ? 1 : -1)

instead of

std::pow(-1, i)

?

Full answer:

double sum = 0;
int i = 1.0;
int n = 5.0;
for (i = 1; i <= n; ++i) {
    signed char sign = ((i % 2) == 0 ? 1 : -1);
    sum += sign * (i / (i+1));
}
Tomasz Gandor
  • 8,235
  • 2
  • 60
  • 55
2

Few problems:

  1. ^ is teh bitwise exclusive or in c++ not "raised to power". Use pow() method.

  2. Remove the dangling opening bracket from the last line

  3. Use ints not floats when assigning to ints.

Sid
  • 7,511
  • 2
  • 28
  • 41
2

You seem to have a few things wrong with your code:

using namespace std;

This is not directly related to your problem at hand, but don't ever say using namespace std; It introduces subtle bugs.

int i = 1.0;
int n = 5.0;

You are initializaing integral variables with floating-point constants. Try

int i = 1;
int n = 5;


sum = (-1)^i*(i/(i+1));

You have two problems with this expression. First, the quantity (i/(i+1)) is always zero. Remember dividing two ints rounds the result. Second, ^ doesn't do what you think it does. It is the exclusive-or operator, not the exponentiation operator. Third, ^ binds less tightly than *, so your expression is:

-1 xor (i * (i/(i+1)))

-1 xor (i * 0)
-1 xor 0
-1
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
1

^ does not do what you think it does. Also there are some other mistakes in your code.

What it should be:

#include <iostream>
#include <cmath>

int main( )
{
    long sum = 0;
    int i = 1;
    int n = 5;

    for( i = 1; i <= n; i++ )
        sum += std::pow( -1.f, i ) * ( i / ( i + 1 ) );

    std::cout << "Sum = " << sum << std::endl;

    return 0;
}

To take a power of a value, use std::pow (see here). Also you can not assign int to a decimal value. For that you need to use float or double.

The aforementioned ^ is a bitwise-XOR, not a mark for an exponent.

Also be careful of Integer Arithmetic as you may get unexpected results. You most likely want to change your variables to either float or double.

ssell
  • 6,429
  • 2
  • 34
  • 49
1

There are a few issues with the code:

int sum = 0;

The intermediate results are not integers, this should be a double

int i = 1.0;

Since you will use this in a division, it should be a double, 1/2 is 0 if calculated in integers.

int n = 5.0;

This is an int, not a floating point value, no .0 is needed.

    for(i=1;i<=n;i++)

You've already initialized i to 1, why do it again?

sum = (-1)^i*(i/(i+1));

Every iteration you lose the previous value, you should use sum+= 'new values'

Also, you don't need pow to calculate (-1)^i, all this does is switch between +1 and -1 depending on the odd/even status of i. You can do this easier with an if statement or with 2 for's, one for odd i one for even ones... Many choices really.

Andrei
  • 4,880
  • 23
  • 30