I'm trying to translate this series into a cod that give me the sum of it. here is the series: X + (-1/3)X^3 + (1/5)X^5 + (-1/7)X^7 + …. +((-1)^(n-1)/2n-1)X^(2n-1)
that's the cod (by the way I can't use cmath library..):
#include <iostream>
using namespace std;
int main()
{
cout << "enter 2 numbers: \n";
int x, n; // x - the base. n - the number of the series organs.
cin >> x;
do
{
cin >> n;
if (n <= 0) cout << "ERROR \n";
} while (n <= 0);
int i{ 1 }; // index for the first organ, and so on.
int denom{ 2 * i - 1 }; // the denominator and the power in the equation.
int factor; // the factor for X.
int xValue{ x }; // the X value after the power.
int sum{ 0 }; // the sum of the series.
int an; // General organ in the series.
for (; i <= n; ++i)
{
if ((i - 1) % 2 == 0) // == positive cardinal case.
{
factor = (1 / denom);
for (; i <= denom;)
{
xValue *= xValue;
}
an = factor * xValue;
sum += an;
xValue = x;
}
else // == negetive cardinal case.
{
factor = (-1 / denom);
for (; i <= denom;)
{
xValue *= xValue;
}
an = factor * xValue;
sum += an;
xValue = x;
}
}
cout << sum;
}
why the cod don't show me the sum? thank you all very much!!
Aucun commentaire:
Enregistrer un commentaire