Trouble with simpson's rule in a Programming Assignment

Thread Starter

chinesebarbiedoll

Joined Nov 9, 2011
9
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.:confused:
Here is the code:
Rich (BB 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;
            
            }
 

Thread Starter

chinesebarbiedoll

Joined Nov 9, 2011
9
This is my new function for the code but now it gives me 665000 cubic units.
Rich (BB 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;
            
            }
 
Top