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.