-
Notifications
You must be signed in to change notification settings - Fork 0
/
trap.c
35 lines (32 loc) · 792 Bytes
/
trap.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double *allocate(int n)
{
double *p = (double*)malloc(n*sizeof(double));int i=0;
for(i=0; i<n; i++)
p[i] = 0;
return p;
}
double func(double x){ return x/(1+x); }
int main()
{
int n, i=0;
printf("Enter n.");
scanf("%d", &n);
printf("Enter Xs in n folowing lines:");
double *x = allocate(n), *y = allocate(n);
/* for(int i=0;i<n;i++) { y[i] = func(x[i]);}*/
double sum1=0, sum2=0;
for(i=0; i<n; i++)
{
scanf("%lf", &x[i]);
y[i] = func(x[i]);
if(i==0 || i==n-1)
sum1 +=y[i];
else sum2 += y[i];
}
double h = x[1]-x[0];
double res = h/2.0*(sum1 + 2*sum2);
printf("\n%lf + 2(%lf) = %lf", sum1, sum2, res);
}