-1

I want to find the sum up to the 'n'th term for the following series:

(1/2)+((1*3)/(2*4))+((1*3*5)/(2*4*6))....

So, I wrote the following program in c++ :

#include <bits/stdc++.h>
#include <conio.h>
using namespace std;
int main()
{
int p=1, k=1, n=0;
float h=0;
cout<<"Enter the term: ";
cin>>n;
for(int i=1; i<=n; i++)
{
    for(int j=1; j<=i; j++)
    {
        p*=((2*j)-1);
        k*=(2*j);
    }
    h+=(p/k);
    p=1;
    k=1;

}
cout<<"The sum is : "<<h;
return 0;
getch();

}

However, the output of the program always gives me '0'. I can't figure out the problem with the program.

N.B. I'm new to programming.

samuelnj
  • 1,627
  • 1
  • 10
  • 19
  • 1
    The expression `p/k` with `int p` and `int k` looks like a bad idea in the context of what you're hoping to achieve, since it gives an integer result (for example, `1/2 == 0`). You might want to use `float` or `double` instead... – goodvibration Nov 22 '17 at 17:02
  • 2
    try `(float)p / k` instead of `p/k` – Jay Joshi Nov 22 '17 at 17:04
  • 1
    Debugger. Use a debugger. A debugger will allow you to single-step through the code, *watching* values of variables. Often, debugging is faster than creating a correct post to StackOverflow and waiting for somebody to inspect your code or debug your code for you. – Thomas Matthews Nov 22 '17 at 17:47
  • Just change `h+=(p/k)` to `h+=((p*1.0)/k)` so that p/k will be converted to floating point value. – Ishpreet Nov 23 '17 at 06:58

2 Answers2

0

The problem here is that you haven't declared p and k as float or doubleor explicitly cast them as such before the calculation and assignment to h.

What's happening is for every iteration of the loop p < k (by nature of the problem) since p and k are both declared as int, p / k = 0. So you're just summing 0 for every iteration.

Either declare p and k as float or double or do this:

h += ((float) p) / ((float) k)

Also, for this specific problem I assume you're looking for precision, so be wary and look into that as well Should I use double or float?

samuelnj
  • 1,627
  • 1
  • 10
  • 19
0

implicit conversion and type casting are a trap where all newbies fall. in the instruction:

h += p/k;

the compiler performs an integer division first, then a promotion of the result to floating point type. and since:

p < k ; for all i,j < n

then:

res = (p / k) < 1 => truncates to 0;  // by integer division

thus:

sum(1->n) of p/k = sum (1->n) 0 = 0;

finally:

h = conversion to float of (0) = 0.0f;

that's why you have the result of 0.0f at the end.

the solution:

1- first of all you need to use the natural type for floating point of c++ which is "double" (under the hood c++ promotes float to double, so use it directly).

2- declare all your variable as double, except the number of terms n:

3- the number of terms is never negative, you need to express that in your code by declaring it as an unsigned int.

4- if you do step 3, make sure to catch overflow errors, that is if the user enters a negative number your risk to have a very big number in "n", expel : n =-1 converts to 0xffffffff positive number.

5- engineer your code sometimes is better.

6- include only the headers that you need, and avoid a importing any namespace in your global namespace.

here is how i think you should write your program.

#include <iostream>

double sum_serie(unsigned int n)
{
    double prod = 1.0, sum = 0.0;
    for (double c=1; c<=n ; c++)
    {
         prod *= ( ( 2*c ) - 1 ) / ( 2*c ); // remark the parenthesis
         sum += prod;
    }
    return sum;
}

int main()
{
    unsigned int n = 0;
    int temp = 0;
    std::cout << " enter the number of terms n: ";
    std::cin  >> temp;
    if (temp > 0) 
        n = temp; // this is how you catch overflow
    else
    {
        std::cout << " n < 0, no result calculated " << std::endl;
        return 0;
    }
    std::cout << " the result is sum = " << sum_serie(n) << std::endl;
    return 0;
}

I know that the question was about the implicit conversion and casting in C++, but even the way of writing a code can show you what bugs you have in it, so try to learn a proper way of expressing your ideas into code, debugging comes natural afterward.

Good Luck

organicoman
  • 154
  • 6