Thread: trapezoidal rule

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    trapezoidal rule

    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
    Last edited by howlin; 03-04-2012 at 12:40 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Announcements - General Programming Boards
    Have another go at posting your code, so it isn't on one line.
    Try editing it first, rather than posting again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    How does the code, which seems to be in code tags, end up all on one line?

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    I dont no how i did it, but i have changed it now (hopefully)

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Instead of hard-coding the formula, simply put a 'for' loop indexing the array. But your algorithm still needs help. You aren't using any of the arrayx.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Quote Originally Posted by nonoob View Post
    Instead of hard-coding the formula, simply put a 'for' loop indexing the array. But your algorithm still needs help. You aren't using any of the arrayx.
    I only use the x array for calculate h which is just x1 - x0

    In gerneal how would i do the for loop indexing the array?
    I dont think we have done something like that in class yet (i could be wrong do)

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Well you are already using arrays to input values. Use similar idea for the formula: something like
    ans =(.5)*h*(arrayy[0] + 2*arrayy[1] + 2*arrayy[2] + 2*arrayy[3] + 2*arrayy[4] + 2*arrayy[5] + arrayy[arraysize]);
    Code:
    tot = 0;
    for (i = 0; i < arraysize; i++) {
        total += 2 * arrayy[i];
        }
    ans = 0.5 * h * tot;

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nonoob View Post
    + arrayy[arraysize-1]
    Minus one.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Indeed. I copied from original post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trapezoidal Rule for Integration
    By lolcats22 in forum C Programming
    Replies: 2
    Last Post: 11-20-2011, 02:09 AM
  2. The trapezoidal rule question
    By paranoidgnu in forum C Programming
    Replies: 9
    Last Post: 04-24-2011, 09:00 AM
  3. Simpson's rule and Trapezoidal rule from fixed array
    By timwonderer in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2010, 03:14 PM
  4. composite trapezoidal rule
    By Sam Robinson in forum C Programming
    Replies: 0
    Last Post: 05-19-2003, 10:01 AM
  5. Arrays and the trapezoidal rule
    By ChemistryStar4 in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2003, 09:16 PM