[ create a new paste ] login | about

Link: http://codepad.org/QlnjbdNx    [ raw code | output | fork ]

C++, pasted on Jan 3:
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
 
double S(double x, double err, double &n)
{
    double an  = x;
    double sum = 0;
    for(n = 0; err < fabs(an); n = n + 1)
    {
        sum += an;
        an  *= (-1)*x*x*(2*n + 1)/(2*n + 3.0);
    }
    return sum;
}
 
int main()
{
    double xn, xk, dx, s, n;
    cout<<"xn = ";cin>>xn;
    cout<<"xk = ";cin>>xk;
    cout<<"dx = ";cin>>dx;
    cout<<"|  x  |  Sum  |atan(x)| n |\n";
    for(double x = 0.2; x <= 1.0; x = x + 0.1)
    {
        s = S(x, 1E-4, n);
        cout<<setw(5)<<setprecision(3)<<x<<" | "
            <<setw(5)<<setprecision(3)<<s<<" | "
            <<setw(5)<<setprecision(3)<<atan(x)<<" | "
            <<n<<endl;
    }
//    system("pause");//Для CodeBlocks добавить в инклуды <cstdlib>
    return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
xn = xk = dx = |  x  |  Sum  |atan(x)| n |
  0.2 | 0.197 | 0.197 | 2
  0.3 | 0.291 | 0.291 | 3
  0.4 |  0.38 | 0.381 | 4
  0.5 | 0.464 | 0.464 | 5
  0.6 |  0.54 |  0.54 | 7
  0.7 | 0.611 | 0.611 | 9
  0.8 | 0.675 | 0.675 | 13
  0.9 | 0.733 | 0.733 | 25
    1 | 0.785 | 0.785 | 5e+03


Create a new paste based on this one


Comments: