Hi
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
Code:
#include <stdio.h>
double trapezoidal (double arrayy[], double arrayx[], double h);
int arraysize;
int main (void)
{
int a,b;
double f,arrayx[2],arrayy[100], 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]) ;
}
printf("\n");
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);
system ("pause");
return 0;
}
double trapezoidal (double arrayy[], double arrayx[], double h)
{
double f,ans;
int a,b;
h= arrayx[1] - arrayx[0];
printf("Value for h is:%lf",h);
system ("pause");
ans =(.5)*h*(arrayy[0] + 2*arrayy[1] + 2*arrayy[2] + 2*arrayy[3] + 2*arrayy[4] + 2*arrayy[5] + arrayy[arraysize]); // works out the answer
return ans;
}
Hope it is better now