0

I am writing some code that prints out a sum series using a loop and a function. I intend the equation to look like this

m(i) = (1/2) + (2/3) + ... (i / i + 1)

The problem is that my code always gives me incorrect answers and not printing what it's supposed to. For example, when I input 1 into 1 the answer should be 0.5

This is my code:

#include <iostream>
using namespace std;

void sumSeries(int x);

int main() {

    sumSeries(1);

   return 0;
}


void sumSeries(int x){

    double sum = 0;

    for(int i = 0; i < x; i++){

    sum = (x/x + 1);

    sum += sum;

    }

    cout<<sum;

}
Cyrus
  • 49
  • 1
  • 1
  • 7
  • 4
    Several issues. `x/x + 1` always equal 2. Integer division -> `1/2 = 0`. `sum += sum;` means you only keep the last term calculated ... – Damien Mar 25 '21 at 08:40
  • 1) `x` is an integer value. `(x/x + 1)` is also an integer value (always 2). You need to calculate with floating point values when you want to have floating point results (e.g. `sum = (double(x)/(x + 1));`). 2) you are overwritting your sum variable in the loop with every iteration. – Simon Kraemer Mar 25 '21 at 08:42
  • I'm guessing you meant `x/(x+1)`? – Alan Birtles Mar 25 '21 at 08:42
  • After fixing above mentioned issues, your function will behave in an unintended way for `sumSeries(-1)`. Either use `unsigned` or handle negative input properly – gkhaos Mar 25 '21 at 08:44

4 Answers4

1

I see two problems in your code.

First: (x/x+1) != (x/(x+1)), in this case C++ obeys the normal point before line calculation rules.

Second: You are overwriting your sum in each iteration, instead of that you should direct add to sum: sum+=x/(x+1)

And a third issue, as noted by Simon Kraemer, is that you are using integer division, to get the correct results you must cast at least one of the operands to a floating point number.

gerum
  • 974
  • 1
  • 8
  • 21
  • Also `(x/(x+1))` is an integer division. It will always result in `0` as long as `x` is an integer – Simon Kraemer Mar 25 '21 at 08:47
  • 1
    You are right, I have not recognised, that there is integer arithmetic in the calculation. I refered to the evaluation order. – gerum Mar 25 '21 at 12:49
1

Indeed, you overwrite your sum but also take care of your integer division. You may change it as sum += i/(double)(i + 1);

#include <iostream>
using namespace std;

void sumSeries(int x);

int main() {

    sumSeries(5);

   return 0;
}


void sumSeries(int x){
    if (x<0)
    {
        return;
    }

    double sum = 0;

    for(int i = 0; i < x; i++){

        sum += i/(double)(i + 1);

    }

    cout<<sum;

}
Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27
0

What you want is:

void sumSeries(int x){

    double sum = 0;

    for(int i = 1; i <= x; i++){                 // include i in the list
        sum += static_cast<double>(i)/(i + 1);   // force the operation as double
    }
    cout<<sum;
}
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

your mathematical expression has something not normal. Do you mean M(i)= sum(1-i){i/i+1}? , or 1/2 and 1/3 are constants?

in your case as gerum answered it is a small Operator Precedence problem to learn how the C++ compiler prioritize the operators follow here.

your function also should have a guard against zero denominator (undefined values).

Also you should observe that you take int/int division which will ignore the remaining value. then you should consider that by converting the numerator or the denominator to double before the division here .

then your code should be:

#include <iostream>
using namespace std;

void sumSeries(int x);

int main() {

    sumSeries(1);

   return 0;
}


void sumSeries(int x){

    double sum = 0;

    for(int i = 0; i < x; i++){

      if ((x+1)!=0){
        sum += (double)x/(x + 1);
      }
      // the else will apply only if x==-1
      else {
         cout<<"the denominator is zero"<<endl;
         throw;
      }

    }

    cout<<sum;

}
Ibrahim zawra
  • 317
  • 1
  • 2
  • 11