Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define f(x) x*x*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 odd_sum=0,even_sum=0;
- for(int i=1 ; i<n-1 ; i++){
- if(i%2==0)
- even_sum += f(x[i]);
- else
- odd_sum += f(x[i]);
- }
- float ex_ord = f(x[0]) + f(x[n-1]); // extreme ordinates
- float ans = h/3 * ( ex_ord + (4*odd_sum) + (2*even_sum) ) ;
- printf ("\nFinal answer is %f",ans);
- return 0;
- }
- /*
- Enter the values of upper limit,lower limit and h
- 3 -3 1
- Final answer is 98.000000
- */
- // Alogorithm
- 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 even and odd ordinates
- start loop from i=1 to n-2
- check if i%2==0
- if yes
- then
- even_sum = even_sum + f(x[i])
- if no
- else
- odd_sum = odd_sum + f(x[i])
- end i loop
- ex_ord = f(x[0]) + f(x[n-1]) , sum of extreme ordinates
- ans = h/3 * (ex_ord + (4*odd_sum) + (2*even_sum) )
- print ans
- stop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement