-
Notifications
You must be signed in to change notification settings - Fork 0
/
m2.10.c
84 lines (70 loc) · 1.46 KB
/
m2.10.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
typedef enum {
ok,
bad_alloc,
inv_n,
inv_eps
} st_code;
st_code calc_polynomial(double x0, double* coefs, size_t n, double* res) {
*res = 0;
for (int i = 0; i <= n; ++i) {
*res *= x0;
*res += coefs[n - i];
}
return ok;
}
st_code calc_differ(double* coefs, size_t n, int iter) {
for (int j = 0; j < n - iter; j++) {
coefs[j] = (j + 1) * coefs[j + 1];
}
coefs[n - iter] = 0;
return ok;
}
st_code tr_polynomial(size_t n, double x0, double eps, double** result_k, ...) {
if (n == 0) {
return inv_n;
}
if (eps <= 0) {
return inv_eps;
}
double* inp_k = (double*) malloc(sizeof(double) * n);
if (inp_k == NULL) {
return bad_alloc;
}
*result_k = (double*) malloc(sizeof(double) * n);
if (result_k == NULL) {
free(inp_k);
return bad_alloc;
}
va_list a;
va_start(a, result_k);
for (int i = 0; i < n; i++) {
inp_k[i] = va_arg(a, double);
}
va_end(a);
for (int i = 0; i < n; i++) {
calc_polynomial(x0, inp_k, n - i, &((*result_k)[i]));
for (int j = 2; j < i + 1; j++) {
(*result_k)[i] /= j;
}
calc_differ(inp_k, n, i);
}
free(inp_k);
return ok;
}
int main() {
double* coefs;
int n = 4;
if(tr_polynomial(n, 0, 1, &coefs, 5.0, 11.0, 3.0, 8.0) != ok){
printf("Something is off\n");
return 1;
}
for (int i = 0; i < n; i++){
printf("%lf ", coefs[i]);
}
free(coefs);
putchar('\n');
return 0;
}