Thread: The Trapezoidal Rule for C programming C++ question. Anyone knows?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    17

    The Trapezoidal Rule for C programming C++ question. Anyone knows?

    I am new to C programming. Anyone has the code for this problem below or how to do it?

    I am trying to write a program in Putty that using Trapezoidal rule that reads a,b and N then calculates approx area for this function f(x)= 2x^2 +x

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Read our Homework Policy. We help people find and repair bugs in their code, we don't hand out free code like candy. Attempt the problem yourself and then, if you get stuck, come back and we will help you.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    Hi, I wrote that program but its wrong when I calculate it by hand. What else should I do? anything missing?
    Code:
    /* trapezoid.c */
    1. include <stdio.h>
    2. include <math.h>
    float f(float); float a; float b; float x; float h; float sum; int n; int i; int main() { printf("Enter value for a: "); scanf("%f", &a); printf("Enter value for b: "); scanf("%f", &b); printf("Enter number of rectangles: "); scanf("%d", &n); h = (b - a) / n; sum = (0.5 * h) * (f(a) + f(b)); printf("%f\n", sum); for (i = 1; i < n; i++) { sum = sum + h * f(a + (i * h)); printf("%f\n", sum); } printf("The value of the integral is: %f\n", sum); } float f(float x) { float value; /* define function here */ value = 2(x*x) + x; return value; }
    Whats wrong?

  4. #4
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Quote Originally Posted by TheBigH View Post
    we don't hand out free code like candy.
    Maybe for money tho...

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Okay, the first problem is your 2(x*x)+x. You need to stick another * between the 2 and the brackets.
    Code:
    while(!asleep) {
       sheep++;
    }

  6. #6
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    What should a sample input and answer be?

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    Thats the only one? or I am wrong in the whole calculation?

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    well, I added the * between 2 and (x*x) and ran the program again. I am still not getting the answer. I entered 2 for a then 3 for b then 5 for n and I got the answer 14.68 but I have 15.something when I do it on calculator. Do you know whats my problem? Maybe I am doing wrong on calculator. idk

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    The answers looked reasonable to me. Tell us what input you gave, what output you get and what output you expect to get. We can't very well tell you what's wrong otherwise.

    Also, I took the liberty of adding the fix TheBigH suggested and cleaning up some of your formatting so your code is a little more readable. Keep to one statement per line, and make sure subordinate lines of code are indented. Really, that goes a long way in spotting errors or potential errors (not that I happened to see any in this case, but for the future)
    Code:
    /* trapezoid.c */
    #include <stdio.h>
    
    
    #include <math.h>
    
    
    float f(float);
    float a;
    float b;
    float x;
    float h;
    float sum;
    int n;
    int i;
    
    
    int main() {
        printf("Enter value for a: ");
        scanf("%f", &a);
        printf("Enter value for b: ");
        scanf("%f", &b);
        printf("Enter number of rectangles: ");
        scanf("%d", &n);
    
    
        h = (b - a) / n;
        sum = (0.5 * h) * (f(a) + f(b));
        printf("%f\n", sum);
        for (i = 1; i < n; i++) {
            sum = sum + h * f(a + (i * h));
            printf("%f\n", sum);
        }
        printf("The value of the integral is: %f\n", sum);
    }
    
    
    float f(float x) {
        float value;
    
    
        value = 2*(x*x) + x;
    
    
        return value;
    }
    A few other notes:
    • Avoid global variables whenever possible. You can (and should) move all of those variables inside main(). Read more about it: Global Variables Are Bad.
    • You said main would return an int, do so. Zero is typical for success.
    • What is the purpose of the line in red? That looks like area of a triangle, and inside your loop you add up the area of rectangles. I think you need to ditch the line in red and use the area of a trapezoid formula inside your loop, 0.5 * (b1 + b2) * h.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by kevave View Post
    well, I added the * between 2 and (x*x) and ran the program again. I am still not getting the answer. I entered 2 for a then 3 for b then 5 for n and I got the answer 14.68 but I have 15.something when I do it on calculator. Do you know whats my problem? Maybe I am doing wrong on calculator. idk
    The trapezoid method, or whatever method you're using in your code, is just an approximation. Your calculator answer may be off a tiny bit. Try changing n to 5000 in your code and see what happens.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The trapezoidal rule question
    By paranoidgnu in forum C Programming
    Replies: 9
    Last Post: 04-24-2011, 09:00 AM
  2. 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
  3. C programming help0 Simpson Rule
    By rasikan in forum C Programming
    Replies: 8
    Last Post: 09-22-2010, 10:06 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

Tags for this Thread