Trouble with Simpson's rule in my program

This is a discussion on Trouble with Simpson's rule in my program within the C++ Programming forums, part of the General Programming Boards category; I am having trouble with my program. I have been working on it for the last couple of days. I ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Trouble with Simpson's rule in my program

    I am having trouble with my program. I have been working on it for the last couple of days. I am supposed to use Simpson's rule but I am having trouble implementing it into my program.
    You have to input:
    road length= 1000
    road width= 75
    measure points= 11
    enter for y value : 0, 6, 10, 13, 17, 22, 25, 20, 13, 5, 0
    the answer is supposed to be 985000 cu. ft. but I keep getting 982500.0 cu. ft.
    Here is the code:
    Code:
    #include <iostream>
    #include <conio.h>
    #include <math.h>
    #include <iomanip>
    using namespace std;
    // Prototyptes
    double SimsonArea(int, double);
    int main()
    {
        int NumPoints, RdWidth;
        double RdLength, HillCrossSectionalArea, DirtVolume;
        char Usrp;
        
        do
        {
             system("CLS");
             cout << "\n\n";
       
             cout <<"\nEnter the length of roadway through the hill:";
             cin >> RdLength;
             cout <<"\n      Enter the width of the roadway:";
             cin >> RdWidth;
             cout <<"\nEnter the number of points at which hill";
             cout <<"\n          elevation was measured:";
             cin >> NumPoints;
             cout <<"\nEnter the hill elevations(y values) at the";
             cout <<endl << NumPoints << " equally-spaced points:";
             cout <<"\n\n";
             
             HillCrossSectionalArea = SimsonArea(NumPoints,RdLength);
             
             DirtVolume = RdWidth * HillCrossSectionalArea;
             
             cout << setprecision(1) << fixed << showpoint;
             cout << "\n\n";
             cout << "---> The volume of dirt to be removed is";
             cout << "\n  Approximately " << DirtVolume << " cubic units.";
             
             cin.ignore();
             cout << "\n\n\n\t Do Another (Y or N):";
             cin.get(Usrp);
             Usrp = toupper(Usrp);
             
             } while (Usrp == 'Y');
             
             }
             
             
            
             
             double SimsonArea (int n, double length)
             {
                    double sum = 0, yValue, deltaX;
                    
                for(int i = 0; i <= n-1; i++)
                {
                        cout <<"Enter y value #" <<i+1 <<":";
                        cin >> yValue;
                        if(i==0 || i==n)
                           sum += yValue*4.0;
                          else
                            sum+=yValue;
                }
                
                deltaX=length/static_cast<double>(n-1);
                return deltaX*sum;
                
                }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    New function

    This is my new function for the code but now it gives me 665000 cubic units.
    Code:
    double SimsonArea (int n, double length)
             {
                    double sum = 0, yValue, deltaX;
                    
                for(int i = 0; i <= n-1; i++)
                {
                        cout <<"Enter y value #" <<i+1 <<":";
                        cin >> yValue;
                        
                        if(i==0 && i==n)
                           sum+=yValue*4.0;
                          else
                            sum+=yValue*2.0;
                          
                }
                deltaX=length/static_cast<double>(n-1);
                deltaX/=3.0;
                return deltaX*sum;
                
                }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simpson 3/8 rule
    By Matts in forum C Programming
    Replies: 3
    Last Post: 06-16-2011, 08:23 AM
  2. Simpson's rule and Trapezoidal rule from fixed array
    By timwonderer in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2010, 02:14 PM
  3. C programming help0 Simpson Rule
    By rasikan in forum C Programming
    Replies: 8
    Last Post: 09-22-2010, 10:06 PM
  4. simpson's 1/3rd rule
    By rakeshkool27 in forum C Programming
    Replies: 16
    Last Post: 03-22-2010, 07:40 AM
  5. SImpson's Rule
    By Brent in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 08:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21