Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)

 BobbyK 3Mar2010 20:32

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

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...........

 xpi0t0s 4Mar2010 15:10

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

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].

 virxen 4Mar2010 17:03

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

 BobbyK 4Mar2010 17:58

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

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:

 virxen 5Mar2010 04:22

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

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; }```

 All times are GMT +5.5. The time now is 20:41.