Advertisement
Kentoo

A#1

Jan 16th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #define _USE_MATH_DEFINES
  4. #include "math.h"
  5.  
  6. using namespace std;
  7.  
  8. int fact(int n) {
  9.     switch (n) {
  10.     case 0: return 1;
  11.     case 1: return 1;
  12.     default: {
  13.         int k = 1;
  14.         for (int i = 1; i <= n; i++)
  15.             k *= i;
  16.         return k;
  17.     }
  18.     }
  19. }
  20.  
  21. double receq(double x, double f, double fk, double e, double xi, int i) {
  22.     double t = -1 * xi * x * (i + 1) * fact(i - 1) / (i * fact(i));
  23.     f += t;
  24.     if (abs(f - fk) < e) {
  25.         return f;
  26.     }
  27.     else {
  28.         return receq(x, f, fk, e, t, i + 1);
  29.     }
  30. }
  31.  
  32. void main()
  33. {
  34.     double e;
  35.     cout << "Input accuracy of calculation" << endl;
  36.     cin >> e;
  37.     double x;
  38.     cout << "Input point of equation" << endl;
  39.     cin >> x;
  40.     double k;
  41.     k = x * exp(-x) - exp(-x) + 1;
  42.     double r;
  43.     r = receq(x, 0, k, e, -1, 1);
  44.     cout.precision(15);
  45.     cout << "k = " << k << endl;
  46.     cout << "r = " << r << endl;
  47.     system("pause");
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement