Advertisement
Sanlover

Untitled

Nov 2nd, 2021
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.   double X, E, E10, summary = 0, summaryE = 0, summaryE10 = 0;
  7.   int N, amountE = 0, amountE10 = 0;
  8.  
  9.   cout << "Enter X: ";
  10.   cin >> X;
  11.  
  12.   if (X <= -1 || X >= 1) {
  13.     cout << "X must be in (-1;1).";
  14.     return 0;
  15.   }
  16.  
  17.   cout << "Enter E: ";
  18.   cin >> E;
  19.   if (E < 0) {
  20.     cout << "Your E is negative but must be nonnegative. E will be absoulte.";
  21.     E = abs(E);
  22.   }
  23.   E10 = E / 10;
  24.  
  25.   cout << "Enter N: ";
  26.   cin >> N;
  27.   if (N < 0) {
  28.     cout << "N must be nonnegative";
  29.     return 0;
  30.   }
  31.  
  32.   double term = 1;
  33.   for (int i = 0; i < N; i++) {
  34.     summary += term;
  35.     if (abs(term) > E10) {
  36.       if (abs(term) > E) {
  37.         summaryE += term;
  38.         amountE++;
  39.       }
  40.       summaryE10 += term;
  41.       amountE10++;
  42.     }
  43.     term *= -X;
  44.   }
  45.  
  46.   cout << endl << "Results:" << endl;
  47.   cout << "1/(1+x) = " << 1.0 / (1.0 + X) << endl;
  48.   cout << "Summary of N(" << N << ") elements is equal to " << summary << endl;
  49.   cout << "Summary of (" << amountE
  50.        << ") elements, which absolute value is higher than E(" << E
  51.        << ") equals to " << summaryE << endl;
  52.   cout << "Summary of (" << amountE10
  53.        << ") elements, which absolute value is higher than E10(" << E10
  54.        << ") equals to " << summaryE10 << endl;
  55.   return 0;
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement