Help - Search - Members - Calendar
Full Version: OMG HELP!11!!1one!
Forum > Public > Lobby
Mizzou
Fix my program sad.gif

CODE

#include <iostream>
#include <cmath>
using namespace std;

double calculateArea( double leftLimit, double rightLimit, double tolerance, double area );
double lHeight( double leftLimit );
double rHeight( double rightLimit );


int main()
{
    double leftLimit, rightLimit, tolerance, initialArea, area1;

    cout << "This is a demonstartion of integrating x^3 + 2." << endl;
    cout << "Enter the left & right limits of integration: ";
    cin >> leftLimit >> rightLimit;
    cout << endl;

    cout << "Enter the desired error tolerance: ";
    cin >> tolerance;
    cout << endl;

    //function is f(x) = x^3 + 2
    initialArea = (rightLimit-leftLimit)*(( lHeight( leftLimit ) +  rHeight( rightLimit ))/2 );
    area1 = calculateArea( leftLimit, rightLimit, tolerance, initialArea );
    cout << "The area is approximately " << area1 << endl;

    return 0;
}

double calculateArea( double leftLimit, double rightLimit, double tolerance, double area )
{
    double mid = (leftLimit + rightLimit)/2;
    double area2 = (mid-leftLimit)*(( lHeight( leftLimit ) +  rHeight( mid ))/2 ) +
        (rightLimit-mid)*(( lHeight( mid ) + rHeight( rightLimit ))/2 );
    if( fabs(area-area2) < tolerance )
        return area2;
    else
    {
        return calculateArea( leftLimit, mid, tolerance/2, area2 ) + calculateArea( mid, rightLimit, tolerance/2, area2 );
    }
}

double lHeight( double leftLimit )
{
    return pow( leftLimit, 3 ) + 2;
}

double rHeight( double rightLimit )
{
    return pow( rightLimit, 3 ) + 2;
}
Jonobono
There is a reason i failed out of the last tech school i attended. And this was it. (among other things)
Mizzou
okay so my teacher says....and I quote

QUOTE
You're passing the recursive call the data on the *sum* of the left+right trapezoids, and comparing it to the value of just the left in one call & just the right in the other. The call to calculate the area of the left should get the area for just the left trapezoid, the call to calculate the right should get just the right.
Mizzou
QUOTE(Mizzou @ Jan 28 2010, 02:16 AM) *

return calculateArea( leftLimit, mid, tolerance/2, area2 ) + calculateArea( mid, rightLimit, tolerance/2, area2 );


Next time change the area2 to leftArea & rightArea.....OMG! Stupid easy fix that took me hours to find. sad.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.