Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- float f(float x)
- {
- return 1 / (x+1);
- }
- int main()
- {
- float a, b, epsilon = powf(10, -5), II, IO, h, s;
- int n = 1, counter=1;
- printf("a=>");
- scanf("%f", &a);
- printf("b=>");
- scanf("%f", &b);
- //printf("epsilon=>");
- //scanf("%f", &epsilon);
- II = (f(a) + f(b)) * (b - a) / 2;
- do {
- n *= 2;
- h = (b - a) / n;
- IO = II;
- s = 0;
- for (int i = 1; i <= n - 1; i++)
- s += f(a + i * h);
- II = (f(a) + 2 * s + f(b)) * h / 2;
- counter++;
- } while (fabs(II - IO) >= epsilon);
- printf("The value of the integral part with precision %f is %f in %d steps.\n", epsilon, II, counter);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement