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

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

1. ### BobbyKNew Member

Joined:
Mar 2, 2010
Messages:
3
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. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
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. ### virxenNew Member

Joined:
Nov 24, 2009
Messages:
387
90
Trophy Points:
0
4. ### BobbyKNew Member

Joined:
Mar 2, 2010
Messages:
3
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. ### virxenNew Member

Joined:
Nov 24, 2009
Messages:
387
90
Trophy Points:
0
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;
}
```