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

BobbyK's Avatar, Join Date: Mar 2010
Newbie Member
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...........
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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].
BobbyK like this
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
check this

http://en.wikipedia.org/wiki/Numerical_integration
BobbyK like this
0
BobbyK's Avatar, Join Date: Mar 2010
Newbie Member
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????????????
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
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;
}
BobbyK like this