C++ Help Please.......Area under a curve

Discussion in 'C++' started by BobbyK, Mar 3, 2010.

  1. BobbyK

    BobbyK New Member

    Joined:
    Mar 2, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    hey all...........

    Im doing an assignment for college and i need help writing a simple program for C++
    its writing a program to calculate the area under a curve and im reallly stuck any help would be brillant. i dont really no how to approach it either. even if u could just copy and paste an actual workable solution to the problem i would be very greatful...........



    cheers...........
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    How is the curve defined? If you have an equation for it, e.g. y=x^2+3x+5, then you can calculate the area via the integral [Int(y)=x^3/3+3x^2/2+5x].
     
  3. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
  4. BobbyK

    BobbyK New Member

    Joined:
    Mar 2, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    for example if it was defined for y=x^2

    I honestly dont have a clue how to write a basic C++ program for it........
    i have to use both rectangles and trapezoids methods to find the area????????????
    :undecided:worried:
     
  5. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    complex trapezoid method for f(x)=x^2 only
    =============================

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    double f(float);
    
    int main(){
        float xo,x;
        float a,b,h;
        double integral;
        int n;
        printf("\ngive left x value;:");
        scanf("%f",&a);
        printf("\ngive right x value;:");
        scanf("%f",&b);
        printf("\nenter how many sections you want(bigger more accurate result):");
        scanf("%d",&n);
        //calculations
        h=(b-a)/n;
        integral=(f(a)+f(b))/2;
        for (int i=1;i<n;i++){
            integral+=f(a+i*h);
        }
        integral=integral*h;
        printf("\n the integral of f(x)=x^2,from a=%3f , to b=%3f is %6.3f",a,b,integral);
        getchar();    getchar();
    }
    double f(float x){
    return x*x;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice