Initial task:

Given the real numbers X, E. Calculate the amount up to E:

alt text

#include<stdio.h> #include<conio.h> #include<math.h> main(void) // { // int x, n, i; // i-переменная счетчика цикла float s, ch, e, f; // s-сумма членов сходящегося ряда ch, f-факториал printf("nnEnter number X: "); scanf("%f", &x); // Ввод числа X printf("nnEnter precision: "); scanf("%f", &e); // Ввод точности расчета n = 1; // Начальное значение аргумента для вычисления члена ряда ch = 1; // Начальное значение члена ряда s = 0; // Присвоение начального значения рекуррентного выражения // накопления суммы while (ch > e) // Цикл выполняется до тех пор, пока очередное значение { // выражения больше заданной точности f = 1; // Присвоение начального значения рекуррентного выражения // вычисления факториала for (i = 1; i <= n; i++) f *= i; // Вычисление факториала ch = pow(-1, n) * (pow(x, (2 * n + 1))) / (f * (2 * n + 1)); // Вычисление выражения s += ch; // Накопление суммы n++; // Переход к следующему члену ряда } printf("Сумма равна s=%f", s); // Вывод полученного значения // суммы на экран getch(); // Задержка экрана до нажатия любой клавиши } 
  • and what specifically does not work? - Stas0n
  • If we take the number 5 as X, for example, and specify 1 as accuracy (that is, count to 1), then the result of the program is as follows Enter number X: 5 Enter precision: 1 Summ is s = -41143894683162890000000000000.000000 There is an error somewhere in the condition, but can not understand exactly where. In C is not very strong ... - denmmx

2 answers 2

I would do this:

 #include <iostream> #include <stdio.h> #include <math.h> #include <clocale> using namespace std; int fact(int k); double E; int k = 0; float Summ = 1, x, F; void main() { setlocale(0, "Rus"); cout << "Введите x" << endl; do { cin >> x; if (x == 0) cout << "Ошибка: x = 0, Введите данные заново!" << endl; } while (x == 0); cout << "Введите Е" << endl; do { cin >> E; if ((E < 0) || (E >= 1)) cout << "Ошибка, Введите данные заново!" << endl; } while ((E < 0) || (E >= 1)); double f = 1; while (true) { f *= -x * x * (2 * k + 1) / (2 * k * k + 5 * k + 1); // по признаку Даламбера рассчитан коэффициент на который отличаются следующий член ряда от предыдущего if (fabs(double (f)) < E) break; else Summ += f; // если точность больше чем член ряда, то выход из цикла, иначе накапливаем сумму cout << "Локальная сумма: " << f << endl; k++; } cout << "Конечная сумма: " << Summ << " полученная за " << k << " шагов." << endl; system("pause"); }; 
  • Thanks for the proposed solution, but you need an option on pure C (not C ++) - denmmx
  • one
    The options differ only in input-output - alexlz
  • alexlz, do not agree! The variant one is solved what is called "head on" with a bunch of not gentle, repetitive calculations: ch = pow (-1, n) * (pow (x, (2 * n + 1))) / (f * (2 * n + one)); mine, with the use of the dalamber sign, is more compact and optimized: f * = -x * x * (2 * k + 1) / (2 * k * k + 5 * k + 1); - Zakhar Zolotarev
  • @Zahar Zolotarev I am sorry. Expressed very clumsily. I answered a replica about C / C ++, i.e. Your option for C is rewritten by replacing I / O. - alexlz
  • alexlz, nothing terrible, who does not happen. - Zakhar Zolotaryov

In general, you should look for the "effect" before doing frontal calculations

sqr (pi) * erf (x) / 2

where erf (x) is the error integral. The task has been reduced to finding it with a given accuracy.