Power & sigma

hi I'm trying to figure out how to make this to work
basically user need to enter numer (top of sigma) and the program should calculate the result
the formula for sigma is from j=0 to= m (user input)
1+j/(2jpower3)+3

so far I got this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include <string>
#include<math.h>

using namespace std;

int main()
{


double n, sum=0, j=0;
    cin>>n;
    for (double i=1; i<=n; i++)
        sum+=(1+j)/(pow(2j3)+3);
    cout<<s;



return 0;
}
pow takes 2 values. pow(base, exponent), eg pow(2,3) is 8.
j appears to be zero and never change.
c++ does not understand shorthand of math.
2*j*3 is valid, 2j3 is nothing, its not even a legal variable name. EVERYTHING is explicit, comparisons, math terms, etc ... no implicit multiply without a * and so on.

its <cmath> not math.h. math.h is a C header and not complete for c++ (it works, but its not complete).
The letter Σ (capital “Sigma”) is mathematical notation for summation, and is read as such:

      ₁₀
    Σ    x²    →    The summation of x² for x in 1 to 10, inclusive. 
      ˣ⁼¹

You have introduced a variable i and used it in place of the “j” given for the loop part of the summation, but not the formula part.

Also, you are declaring both i and j as doubles, when a summation works over integer values.

Finally, remember that code is very strict about order of operations. If you write 1.0/2+3, C++ does it correctly and computes the division first. If you mean for the sum 2+3 to be the divisor, you must use parentheses: 1.0/(2+3).

The same kind of problem will hit you with powers and multiplication. 2j³ is not just 2j3 — C++ needs you to be explicit about the operators.

It also really helps to use a reference. This site has a pretty good one, even if somewhat outdated. http://cplusplus.com/reference/cmath/pow/


So, rewriting:

1
2
3
4
5
6
7
8
    int m;
    // get m from user here //

    double sum=0.0;
    for (int j=1; j<=m; j++)
    {
        sum+=(1+j) / (2*pow(j,3)+3);
    }

This presumes your formula is:

      ₘ      1 + j
    Σ    ───────
      ʲ⁼⁰   2j³ + 3

EDIT: By the way, in C++ if you divide two integers you will get an integer. For a floating point division, at least one of the operands must be a floating point value. Hence why I wrote 1.0 / 2 above. If I had written 1 / 2 that would have been an integer division, and 1 div 2 is zero.

The pow() function always returns a floating point value, so the divisor in your problem will always be a floating point value as well, making the division a floating point division. Heh.

Hope this helps.
Last edited on
the formula for sigma is from j=0 to= m (user input)
1+j/(2jpower3)+3


There is a singularity at j = 0.
Last edited on
ohh, this is quite complicated :)
Thanks for the web, I really need to read all the math in c++ as it seems that I'll need to work on it more in the future.

The formula that you posted is the one that I'm working on. At least I'm happy that you were able to read my post in the right way!

This is the full code with user's interaction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<cmath>

using namespace std;

int main()
{
 int m;
cout<<"Calculation of S"<<endl;
cout<<"Enter m value for the calculation: "<<endl;
cin>> m;
    double sum=0.0;
    for (int j=1; j<=m; j++)
    {
        sum+=(1+j) / (2*pow(j,3)+3);
    }
cout<<"The result of the calculation is"<<endl;
cout<<"S= ";
cout<< sum;
return 0;
}

Many of these summations have a direct equation that gives the answer without adding it up. You can play with the math to see if you can find one or check the web, if you need speed for doing a lot of them. You can also generate a lookup table in the ranges you need, if you need to repeat the same ones over and over as a part of something more.
Topic archived. No new replies allowed.