Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define f(x) 1/(1+x*x)
- int main()
- {
- float b,a,h;
- int n;
- printf("Enter the values of upper limit,lower limit and h\n");
- scanf("%f %f %f",&b,&a,&h);
- n = (b-a)/h +1 ;
- float x[n];
- for(int i=0 ; i<n ; i++){
- x[i] = a+ (i*h);
- }
- float div_3=0,nondiv_3=0;
- for(int i=1 ; i<n-1 ; i++){
- if(i%3==0)
- div_3 += f(x[i]);
- else
- nondiv_3 += f(x[i]);
- }
- float ex_ord = f(x[0]) + f(x[n-1]); // extreme ordinates
- float ans = (3*h)/8 * ( ex_ord + (3*nondiv_3) + (2*div_3) ) ;
- printf ("\nFinal answer is %f",ans);
- return 0;
- }
- /*
- Enter the values of upper limit,lower limit and h
- 1 0 0.166666
- Final answer is 0.785394
- */
- // Algorithm
- define function
- read a,b and h , where a lower , b upper limit and h size of interval
- n = (b-a)/h + 1
- declare float array x of size of n
- start a loop from i=0 to n-1
- x[i] = a + (i*h)
- end i loop
- calulating the sum of ordinate divisible by 3 and not divisible by 3
- declare div_3 = 0 and nondiv_3 = 0
- start loop from i=1 to n-2
- check if i%3==0
- if yes
- then
- div_3 = div_3 + f(x[i])
- if no
- else
- nondiv_3 = nondiv_3 + f(x[i])
- end i loop
- ex_ord = f(x[0]) + f(x[n-1]) , sum of extreme ordinates
- ans = (3*h)/8 * ( ex_ord + (3*nondiv_3) + (2*div_3) )
- print ans
- stop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement