Recursion Help
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Recursion Help

  1. #1
    Join Date
    Jan 2010
    Posts
    4

    Recursion Help

    I cant get this function to recursively call itself what do i have in wrong i go through debugger and it just slides right over the recursive call

    double recursive (double left, double permaRight,double permaTol, double right, double tol)
    {
    double leftArea, rightArea, fullArea, areaAB, finalArea=0, recursiveAreaDifference=0, midpointer;
    leftArea = funcArea(F(left), F(midpoint(left,right)), midpoint(left,right),left);
    rightArea = funcArea(F(right), F(midpoint(left,right)), midpoint(left,right),right);
    fullArea = funcArea(F(left),F(right),(midpoint(left,right)*2),0);
    areaAB = (leftArea +rightArea);
    midpointer = midpoint(left,right);
    finalArea += areaAB;

    if(left == permaRight)//if the changing left bound = the user defined right limit
    {
    return finalArea; //progressive addition of the area
    }

    if((areaAB) <= ((fullArea + tol)||(fullArea -tol)))
    {
    permaTol -( fabs(areaAB - fullArea));
    recursiveAreaDifference += fabs((areaAB - fullArea));
    left = right;
    right = permaRight;
    return (recursive(left,permaRight,permaTol, right, tol));
    }

    else
    {
    return recursive(left,permaRight, permaTol, midpoint(left,right), tol/2);
    }

  2. #2
    Join Date
    Jan 2010
    Posts
    4

    Full Program

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

    double F(double x);
    //PRE: none.
    //POST: returns F(x).
    double midpoint(double a, double b, int c=1);
    double funcArea(double a, double b, double midpoint, double limit);
    double recursive(double left, double permaRight,double permaTol, double right, double tol);

    int main()
    {
    ofstream fout;//opens output file
    fout.open("Output.txt");//designates output file

    if(fout.fail())
    {
    cerr << "output file failed to open" <<endl;

    }

    ///this section just deals with user input of variables to be manipulated
    double left = 0, right = 0, usertol, tol = 0, permaRight =0;

    cout << "Please enter left lateral limit." << endl;
    //cin >> left;

    left = 0; //testing conditions

    fout << "Left limit is " << left << endl;
    cout << "Left limit is " << left << endl;

    cout << "Please enter right lateral limit." <<endl;
    //cin >> right;

    right = 4; ///testing conditions
    permaRight = right;// will keep the right limit stored for the recursive function to check against
    fout << "Right limit is " << right << endl;
    cout << "Right limit is " << right << endl;


    cout << "Please enter area accuracy tolerance." <<endl;
    //cin >> usertol;

    usertol = .1;
    tol = usertol/2; ///testing conditions

    fout << "Tolerance within " << tol <<endl;
    cout << "Tolerance within " << tol <<endl;

    double leftArea =0, rightArea=0, fullArea=0, areaAB=0;
    leftArea = funcArea(F(left), F(midpoint(left,right)), midpoint(left,right),left);
    rightArea = funcArea(F(right), F(midpoint(left,right)), midpoint(left,right),right);
    fullArea = funcArea(F(left),F(right),(midpoint(left,right)*2),0);

    fout << "Midpoint " << midpoint(left,right)<<endl;
    fout << "F(left) "<< F(left)<<endl;
    fout << "F(midpoint) "<< F(midpoint(left,right))<<endl;
    fout << "F(right) "<< F(right)<<endl;
    fout << "Area of left " << leftArea << endl;
    fout << "Area of right " << rightArea <<endl;
    fout << "Full area" << fullArea <<endl;

    fout << "Area estimation" << recursive(left,permaRight,tol ,right,tol)<<endl;


    fout.close();

    return 0;
    }

    double F(double x)
    {
    double z;
    z = pow(x,3)+2;
    return z;
    }
    double midpoint(double a, double b, int c)
    {
    double z;
    z=(a+b)/(pow((double)2,c));
    return(z);
    }
    double funcArea(double a, double b, double midpoint, double limit)
    {
    double z;
    z =((a+b)/2)*(fabs(midpoint - limit)); // a = f(right or left limit) b= f(midpoint)
    //fabs = absolute value of the midpoint
    //limit could be left or right the absolute value function will allow it to work with either
    return z;
    }



    double recursive(double left, double permaRight,double permaTol, double right, double tol)
    {
    double leftArea, rightArea, fullArea, areaAB, finalArea=0, recursiveAreaDifference=0, midpointer;
    leftArea = funcArea(F(left), F(midpoint(left,right)), midpoint(left,right),left);
    rightArea = funcArea(F(right), F(midpoint(left,right)), midpoint(left,right),right);
    fullArea = funcArea(F(left),F(right),(midpoint(left,right)*2),0);
    areaAB = (leftArea +rightArea);
    midpointer = midpoint(left,right);
    finalArea += areaAB;

    if(left == permaRight)//if the changing left bound = the user defined right limit
    {
    return finalArea; //progressive addition of the area
    }

    if((areaAB) <= ((fullArea + tol)||(fullArea -tol)))
    {
    permaTol -( fabs(areaAB - fullArea));
    recursiveAreaDifference += fabs((areaAB - fullArea));
    left = right;
    right = permaRight;
    return (recursive(left,permaRight,permaTol, right, tol));
    }

    else
    {
    return (recursive(left,permaRight, permaTol, (midpoint(left,right)), (tol/2)));
    }
    }

  3. #3
    Join Date
    Jan 2010
    Posts
    4

    Re: Recursion Help

    it executes but horribly need some help if possible

  4. #4
    Join Date
    Jul 2009
    Posts
    37

    Re: Recursion Help

    Easy please
    Put them in code tags then errors will be probably found easier
    Sig-na-tju-(r)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center