Hello everyone, there were difficulties in solving the problem: when entering the value of "ε", the program does nothing. I can not understand where the mistake was made. Please help me fix it. And do I solve it correctly?

enter image description here

The task is as follows: Calculate the infinite sum of a given precision ε (ε> 0). Consider that the required accuracy is reached if the next addend turned out to be less than ε in absolute value.

Directly my "decision"

#include "stdafx.h" #include <iostream> #include "cmath" using namespace std; int main() { double i, z; float s, e, t; cout << "e>0 e="; cin >> e; s = 0; i = 0; z = -1; do { i++; z *= -1; t = z / (i*(i + 1)*(i + 2)); s += t; } while (abs(t)<e); cout << s << endl; system("pause"); return 0; } 
  • why do you think that the program does nothing? tried to use a debugger? - Grundy
  • one
    Cчитать что требуeмая тoчнoсть дocтигнутa,если очередное слагаемое оказалось по модулю меньше ε. Yeah and the amount of 1/n can be calculated ... - pavel
  • one
    @pavel Note that the series is alternating , so this estimate is correct. - Harry

2 answers 2

Your cycle ends when a member has MORE accuracy ...

 #include <iostream> #include <cmath> using namespace std; int main() { double s = 1.0/6.0, e, t = 1.0/6.0; cout << "e>0 e= "; cin >> e; for(int i = 1; fabs(t) > e; ++i) { t *= -i/(i+3.0); s += t; } cout << s << endl; system("pause"); return 0; } 

Just in case - the exact sum of the series - -1.25+2*ln(2)

  • Why sishny fabs if there is std::abs ? - αλεχολυτ
  • @alexolut Habit ... - Harry

Proven solution . Exercised at lunch.

  #include <iostream> #include <cmath> int main() { double accuracyLimit = 0; std::cout << "Enter range accuracy limit: " << std::endl; std::cin >> accuracyLimit; std::cout << "Accuracy limit = " << accuracyLimit << std::endl; double sum = 0; double item = 0; double i = 1; do { auto numerator = std::pow(-1, i); auto denominator = i * (i + 1) * (i + 2); item = numerator / denominator; std::cout << "i=" << i << " item=" << item << " (" << numerator << "/" << denominator << ")" << std::endl; sum += item; ++i; } while (std::abs(item) > accuracyLimit); std::cout << std::endl << " === Calculating finished " << std::endl; std::cout << "Range items count = " << --i << std::endl; std::cout << "Range amount = " << sum << std::endl; } 

Conclusion:

  Enter range accuracy limit: Accuracy limit = 0.001 i=1 item=-0.166667 (-1/6) i=2 item=0.0416667 (1/24) i=3 item=-0.0166667 (-1/60) i=4 item=0.00833333 (1/120) i=5 item=-0.0047619 (-1/210) i=6 item=0.00297619 (1/336) i=7 item=-0.00198413 (-1/504) i=8 item=0.00138889 (1/720) i=9 item=-0.0010101 (-1/990) i=10 item=0.000757576 (1/1320) === Calculating finished Range items count = 10 Range amount = -0.135967 
  • Old-timers, tell me, please. How to make a conclusion without highlighting? - Alexander Petrovskiy
  • remove spaces in front, and choose a different design style in the icons. Or `note or something else. - nick_n_a
  • @AlexanderPetrovskiy Yes, but ... 3 additions, 2 multiplications, 1 division, not counting the exponentiation (!), Where 1 addition, 1 multiplication, 1 division and a sign change are enough. And by the way, the amount is actually positive, you are mistaken with the sign - pow(-1,i+1) needed ... - Harry
  • @harry, yes, wrong sign. Pro performance: Yes, in calculating the series, it is undoubtedly important as nowhere else. But the task, the training, as I understood. In a real application, when profiling, of course you would have to come to your option. But even then, only if this code turned out to be a bottleneck. Otherwise, it is better to read the code better and run longer. For example, if this series is sited only 1 time, at the start of the application, then why profile it. IMHO, of course. - Alexander Petrovskiy