Calculate the sum of an infinite series using user functions. Create two types of programs using external (global) variables and variables passed to and from a function.

x = 0.56 Accuracy = 2 * (10 to the power of -5)

Closed due to the fact that off-topic participants are Vladimir Martyanov , Harry , vp_arth , Kromster , дек 7 Dec '17 at 19:01 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- Harry, vp_arth, Kromster
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Calculate this resource with it? - Vladimir Martyanov
  • one
    What is the "user function"? which user? - Igor
  • one
    Well, since everything is simple, then you know what to do :) - tym32167
  • one
    @Harry "They want to show education"? The man just asked: "how to implement it in code?" - Igor
  • one
    @Igor This is exactly the way to implement - to compare the absolute value of the last member with accuracy. Do you think a person asks how to compare two numbers in a code? :) In general - a pure case of homework without trying to solve. By the way, if I were a teacher, then for one pow(-1, i + 1) I would deploy a solution like yours ... Or for calculating the factorial for each member ... - Harry

3 answers 3

Well, since I am accused of being heartless here :)

 int main() { const double eps = 2e-5; const double x = 0.56; double fact = 2; double ex = exp(x+1); double term = x*x*log(1+x)/(fact + ex); double sum = term; for(int i = 2; fabs(term) > eps; ++i) { term *= -x*log(i+x)*(fact + ex)/log(i-1+x); sum += term /= (fact *= i+1) + (ex *= 2.718281828); } cout << sum << endl; } 
     double calcSum(double x) { int i = 1; double ch = pow(-1, i + 1) * ((pow(x, i + 1) * log(i + x)) / (fact(i + 1) + pow(M_E, i + x))); double sum = 0; while (fabs(ch) > EPS) { sum += ch; i++; ch = pow(-1, i + 1) * ((pow(x, i + 1) * log(i + x)) / (fact(i + 1) + pow(M_E, i + x))); } return sum; } 

      Implemented, but it seems to me that the result is not correct since only one step is performed in the cycle. Who can, check whether all the rules.

       #include <cstdio> #include <cmath> #define EPS (2 * pow(10, -5)) int fact(int x) { return !x || x == 1 ? 1 : fact(x - 1) * x; } double getEx(int i, double x) { return pow(-1, i + 1) * ((pow(x, i + 1) * log(i + x)) / (fact(i + 1) + pow(M_E, i + x))); } double calcSum(double x) { int i = 1; double ch = getEx(i, x); double sum = 0; while (fabs(ch) > EPS) { sum += ch; ch = getEx(++i, x); } return sum; } int main() { double x = 0.56; double sum = calcSum(x); printf("Summa ryada sostavila %.6lf", sum); return 0; } 
      • You have while in a stop condition, not a continuation. - Igor
      • I get the result 0.020633 if I change while to fabs (ch)> = EPS then generally 0.00000 - user8612667