i'm new here so sorry if i put in the code the wrong way
In class on friday I we were doing the trapezoidal rule and the lecturer told us to create a c programe which takes in five values for x and five values for y and then work out the answer.
The equation to is f(x) = .5*h*(f0 + 2*f1 + 2*f2 + ... + 2*fn-1 + fn). (where h is just x1 - x0)
I have created a programe that does it but i want to change it so it can be used for any sized sample.
I have created a matrix that asks how many y values do you want to put in and then puts in the y values.
What i would like some help with is how do i adjust the equation so it can calculate any number of y's instead of just 5?
The code i have below gets you to put in how many y's and but the equation will still only work out 5 y values
Any help would be great
Hope it is better now
double trapezoidal (double arrayy, double arrayx, double h);
int main (void)
double f,arrayx,arrayy, ans, h,j,g;
printf("Enter arraysize: ");
scanf ("%d", &arraysize);
for (a= 0; a < 2; a++)
printf ("Enter a value x%d:", a);
scanf("%lf", &arrayx[a]) ;
for (b= 0; b < arraysize; b++)
printf ("Enter a value y%d:", b);
scanf("%lf", &arrayy[b]) ;
ans = trapezoidal (arrayy,arrayx,h);
printf ("The answer is:%lf\n", ans);
double trapezoidal (double arrayy, double arrayx, double h)
h= arrayx - arrayx;
printf("Value for h is:%lf",h);
ans =(.5)*h*(arrayy + 2*arrayy + 2*arrayy + 2*arrayy + 2*arrayy + 2*arrayy + arrayy[arraysize]); // works out the answer