Name ch_lab1_w cpp Author Warlock include iostream include cmath using

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//============================================================================
// Name : ch_lab1_w.cpp
// Author : Warlock
//============================================================================
#include <iostream>
#include <cmath>
using namespace std;
int fact(int a);
int main() {
double sum = 0;
int i = 1, N = 0;
double e = 0.001;
double dx = 0.05;
double x,a,ft;
cout << "+-----------+--------------+--------------+-----------------------+-------+"<<endl;
cout << "| x | f(x) | ft(x) | (f(x)-ft(x))/f(x)*100 | N |"<<endl;
cout << "+-----------+--------------+--------------+-----------------------+-------+"<<endl;
for(x = 0; x < 1; x += dx)
{
ft = cos(x) - 1;
a = pow((-1),i)*((pow(x,2*i))/(fact(2*i)));
sum = a;
for(i = 2; abs(a) < e; i++, N++)
{
a = pow((-1),i)*((pow(x,2*i))/(fact(2*i)));
sum += a;
}
printf("| %.2lf | %.5lf | %.5lf | %.5lf | %d | \n",x,sum,ft,(sum-ft)/sum*100,N);
// cout << "| " << x <<" "<< sum <<" "<< ft <<" " << (sum-ft)/sum*100 << " " << N <<" |"<<endl;
}
cout << "+-----------+--------------+--------------+-----------------------+-------+"<<endl;
cin.get();
return 0;
}
int fact(int a)
{
if(a > 0)
{
return a*fact(a-1);
}
return 1;
}